mirror of
https://gitee.com/incloudcode/yexuejc-springboot.git
synced 2025-08-07 19:19:31 +08:00
1.1.7 security登录接口开放优化
This commit is contained in:
parent
df5204bc10
commit
0888447168
12
UPDATE.md
12
UPDATE.md
@ -1,6 +1,18 @@
|
|||||||
yexuejc-springboot 更新内容
|
yexuejc-springboot 更新内容
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
#### version :1.1.6-1.1.7
|
||||||
|
**time:2018-11-21 14:18:46** <br/>
|
||||||
|
**branch:** master <br/>
|
||||||
|
**关联工程:** <br/>
|
||||||
|
```
|
||||||
|
springboot-base:1.2.1
|
||||||
|
spring-boot-starter-parent:1.5.16.RELEASE
|
||||||
|
```
|
||||||
|
**update:** <br/>
|
||||||
|
1. security 登录开放处理
|
||||||
|
#
|
||||||
|
|
||||||
#### version :1.1.5
|
#### version :1.1.5
|
||||||
**time:2018年11月20日20:26:06** <br/>
|
**time:2018年11月20日20:26:06** <br/>
|
||||||
**branch:** master <br/>
|
**branch:** master <br/>
|
||||||
|
2
pom.xml
2
pom.xml
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.yexuejc.springboot</groupId>
|
<groupId>com.yexuejc.springboot</groupId>
|
||||||
<artifactId>yexuejc-springboot-parent</artifactId>
|
<artifactId>yexuejc-springboot-parent</artifactId>
|
||||||
<version>1.1.6</version>
|
<version>1.1.7</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.yexuejc.springboot</groupId>
|
<groupId>com.yexuejc.springboot</groupId>
|
||||||
<artifactId>yexuejc-springboot-parent</artifactId>
|
<artifactId>yexuejc-springboot-parent</artifactId>
|
||||||
<version>1.1.6</version>
|
<version>1.1.7</version>
|
||||||
<!-- 本地打包:使用相对关联路径 -->
|
<!-- 本地打包:使用相对关联路径 -->
|
||||||
<!--<relativePath>../../yexuejc</relativePath>-->
|
<!--<relativePath>../../yexuejc</relativePath>-->
|
||||||
</parent>
|
</parent>
|
||||||
|
@ -4,6 +4,7 @@ import com.yexuejc.base.pojo.ApiVO;
|
|||||||
import com.yexuejc.base.util.StrUtil;
|
import com.yexuejc.base.util.StrUtil;
|
||||||
import com.yexuejc.springboot.base.constant.BizConsts;
|
import com.yexuejc.springboot.base.constant.BizConsts;
|
||||||
import com.yexuejc.springboot.base.constant.LogTypeConsts;
|
import com.yexuejc.springboot.base.constant.LogTypeConsts;
|
||||||
|
import com.yexuejc.springboot.base.exception.ClassConvertExeption;
|
||||||
import com.yexuejc.springboot.base.exception.ThirdPartyAuthorizationException;
|
import com.yexuejc.springboot.base.exception.ThirdPartyAuthorizationException;
|
||||||
import com.yexuejc.springboot.base.security.inte.User;
|
import com.yexuejc.springboot.base.security.inte.User;
|
||||||
import com.yexuejc.springboot.base.security.inte.UserService;
|
import com.yexuejc.springboot.base.security.inte.UserService;
|
||||||
@ -177,38 +178,7 @@ public class ConsumerAuthenticationProvider extends AbstractUserDetailsAuthentic
|
|||||||
throw notFound;
|
throw notFound;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
//其他方式登录:查询账号 没有->创建账号
|
third(consumerToken, loadedUser, logtype);
|
||||||
//第三方登录
|
|
||||||
if (consumerToken != null && StrUtil.isNotEmpty(consumerToken.getOpenid())) {
|
|
||||||
ApiVO apiVO = accountView.checkOpenId(consumerToken);
|
|
||||||
if (apiVO.isSucc()) {
|
|
||||||
//已有账号
|
|
||||||
User consumer = apiVO.getObject1(User.class);
|
|
||||||
// 处理用户权限
|
|
||||||
List<GrantedAuthority> authorities = new ArrayList<>();
|
|
||||||
for (String role : consumer.getRoles()) {
|
|
||||||
authorities.add(new SimpleGrantedAuthority(role));
|
|
||||||
}
|
|
||||||
loadedUser = new ConsumerUser(
|
|
||||||
StrUtil.isEmpty(consumer.getMobile()) ? consumerToken.getOpenid() : consumer.getMobile(),
|
|
||||||
consumer.getPwd(), consumer.getEnable(), consumer.getNonExpire(),
|
|
||||||
true, consumer.getNonLock(), authorities, consumer.getConsumerId(),
|
|
||||||
logtype, System.currentTimeMillis());
|
|
||||||
return loadedUser;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//第三方登录+短信登录
|
|
||||||
if (consumerToken != null) {
|
|
||||||
//没有->创建账号
|
|
||||||
consumerToken.isReg = true;
|
|
||||||
ApiVO apiVO = accountView.addConsumer(consumerToken);
|
|
||||||
if (apiVO.isSucc()) {
|
|
||||||
loadedUser = display(consumerToken, apiVO.getObject1(User.class));
|
|
||||||
return loadedUser;
|
|
||||||
} else {
|
|
||||||
throw new ThirdPartyAuthorizationException(apiVO.getMsg());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
if (e instanceof ThirdPartyAuthorizationException) {
|
if (e instanceof ThirdPartyAuthorizationException) {
|
||||||
@ -229,6 +199,69 @@ public class ConsumerAuthenticationProvider extends AbstractUserDetailsAuthentic
|
|||||||
return loadedUser;
|
return loadedUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 第三方登录处理=>登录用户为空,此方法处理返回登录用户
|
||||||
|
*
|
||||||
|
* @param consumerToken 登录信息
|
||||||
|
* @param loadedUser 登录用户(为空时进入此方法)
|
||||||
|
* @param logtype 登录方式
|
||||||
|
* @return 登录用户
|
||||||
|
*/
|
||||||
|
protected UserDetails third(ConsumerToken consumerToken, UserDetails loadedUser, String logtype) {
|
||||||
|
//其他方式登录:查询账号 没有->创建账号
|
||||||
|
//第三方登录
|
||||||
|
if (consumerToken != null && StrUtil.isNotEmpty(consumerToken.getOpenid())) {
|
||||||
|
ApiVO apiVO = accountView.checkOpenId(consumerToken);
|
||||||
|
if (apiVO.isSucc()) {
|
||||||
|
//已有账号
|
||||||
|
Object obj = apiVO.getObject1(Object.class);
|
||||||
|
if (obj instanceof User) {
|
||||||
|
User consumer = (User) obj;
|
||||||
|
// 处理用户权限
|
||||||
|
List<GrantedAuthority> authorities = new ArrayList<>();
|
||||||
|
for (String role : consumer.getRoles()) {
|
||||||
|
authorities.add(new SimpleGrantedAuthority(role));
|
||||||
|
}
|
||||||
|
loadedUser = new ConsumerUser(
|
||||||
|
StrUtil.isEmpty(consumer.getMobile()) ? consumerToken.getOpenid() : consumer.getMobile(),
|
||||||
|
consumer.getPwd(), consumer.getEnable(), consumer.getNonExpire(),
|
||||||
|
true, consumer.getNonLock(), authorities, consumer.getConsumerId(),
|
||||||
|
logtype, System.currentTimeMillis());
|
||||||
|
return loadedUser;
|
||||||
|
} else if (obj instanceof UserDetails) {
|
||||||
|
return (UserDetails) obj;
|
||||||
|
} else {
|
||||||
|
throw new ClassConvertExeption("获取登录用户信息返回结果类型必须是com.yexuejc.springboot.base.security.inte.User实现类" +
|
||||||
|
"或者org.springframework.security.core.userdetails.UserDetails实现类" +
|
||||||
|
"或者com.yexuejc.springboot.base.security.ConsumerUser继承类");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//第三方登录+短信登录
|
||||||
|
if (consumerToken != null) {
|
||||||
|
//没有->创建账号
|
||||||
|
consumerToken.isReg = true;
|
||||||
|
ApiVO apiVO = accountView.addConsumer(consumerToken);
|
||||||
|
if (apiVO.isSucc()) {
|
||||||
|
Object obj = apiVO.getObject1(Object.class);
|
||||||
|
if (obj instanceof User) {
|
||||||
|
User consumer = (User) obj;
|
||||||
|
loadedUser = display(consumerToken, consumer);
|
||||||
|
return loadedUser;
|
||||||
|
} else if (obj instanceof UserDetails) {
|
||||||
|
return (UserDetails) obj;
|
||||||
|
} else {
|
||||||
|
throw new ClassConvertExeption("获取登录用户信息返回结果类型必须是com.yexuejc.springboot.base.security.inte.User实现类" +
|
||||||
|
"或者org.springframework.security.core.userdetails.UserDetails实现类" +
|
||||||
|
"或者com.yexuejc.springboot.base.security.ConsumerUser继承类");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new ThirdPartyAuthorizationException(apiVO.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return loadedUser;
|
||||||
|
}
|
||||||
|
|
||||||
private void prepareTimingAttackProtection() {
|
private void prepareTimingAttackProtection() {
|
||||||
if (this.userNotFoundEncodedPassword == null) {
|
if (this.userNotFoundEncodedPassword == null) {
|
||||||
this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD);
|
this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD);
|
||||||
@ -249,7 +282,7 @@ public class ConsumerAuthenticationProvider extends AbstractUserDetailsAuthentic
|
|||||||
* @param consumer 实际用户信息
|
* @param consumer 实际用户信息
|
||||||
* @return response User
|
* @return response User
|
||||||
*/
|
*/
|
||||||
private UserDetails display(ConsumerToken consumerToken, User consumer) {
|
protected UserDetails display(ConsumerToken consumerToken, User consumer) {
|
||||||
// 处理用户权限
|
// 处理用户权限
|
||||||
List<GrantedAuthority> authorities = new ArrayList<>();
|
List<GrantedAuthority> authorities = new ArrayList<>();
|
||||||
for (String role : consumer.getRoles()) {
|
for (String role : consumer.getRoles()) {
|
||||||
|
@ -82,13 +82,22 @@ public abstract class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
@Bean
|
@Bean
|
||||||
public ConsumerAuthenticationProcessingFilter consumerAuthenticationProcessingFilter(
|
public ConsumerAuthenticationProcessingFilter consumerAuthenticationProcessingFilter(
|
||||||
AuthenticationManager authenticationManager) throws Exception {
|
AuthenticationManager authenticationManager) throws Exception {
|
||||||
ConsumerAuthenticationProcessingFilter filter = new ConsumerAuthenticationProcessingFilter
|
ConsumerAuthenticationProcessingFilter filter = createConsumerAuthenticationProcessingFilter(authenticationManager);
|
||||||
(authenticationManager);
|
|
||||||
filter.setAuthenticationManager(this.authenticationManager());
|
filter.setAuthenticationManager(this.authenticationManager());
|
||||||
loginHodler(filter);
|
loginHodler(filter);
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化 ConsumerAuthenticationProcessingFilter
|
||||||
|
*
|
||||||
|
* @param authenticationManager
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected ConsumerAuthenticationProcessingFilter createConsumerAuthenticationProcessingFilter(AuthenticationManager authenticationManager) {
|
||||||
|
return new ConsumerAuthenticationProcessingFilter(authenticationManager);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 处理登录
|
* 处理登录
|
||||||
@ -103,8 +112,7 @@ public abstract class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint() {
|
public LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint() {
|
||||||
LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint
|
LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint("/login");
|
||||||
("/login");
|
|
||||||
return loginUrlAuthenticationEntryPoint;
|
return loginUrlAuthenticationEntryPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user