diff --git a/UPDATE.md b/UPDATE.md index 913a5e7..10fa902 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -1,6 +1,18 @@ yexuejc-springboot 更新内容 ------------------- +#### version :1.1.6-1.1.7 +**time:2018-11-21 14:18:46**
+**branch:** master
+**关联工程:**
+``` +springboot-base:1.2.1 +spring-boot-starter-parent:1.5.16.RELEASE +``` +**update:**
+1. security 登录开放处理 +# + #### version :1.1.5 **time:2018年11月20日20:26:06**
**branch:** master
diff --git a/pom.xml b/pom.xml index d39707d..a8f23c2 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.yexuejc.springboot yexuejc-springboot-parent - 1.1.6 + 1.1.7 pom ${project.artifactId} diff --git a/yexuejc-springboot-base/pom.xml b/yexuejc-springboot-base/pom.xml index b2e6d97..70d8177 100644 --- a/yexuejc-springboot-base/pom.xml +++ b/yexuejc-springboot-base/pom.xml @@ -9,7 +9,7 @@ com.yexuejc.springboot yexuejc-springboot-parent - 1.1.6 + 1.1.7 diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/ConsumerAuthenticationProvider.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/ConsumerAuthenticationProvider.java index 09494a1..323b064 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/ConsumerAuthenticationProvider.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/ConsumerAuthenticationProvider.java @@ -4,6 +4,7 @@ import com.yexuejc.base.pojo.ApiVO; import com.yexuejc.base.util.StrUtil; import com.yexuejc.springboot.base.constant.BizConsts; 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.security.inte.User; import com.yexuejc.springboot.base.security.inte.UserService; @@ -177,38 +178,7 @@ public class ConsumerAuthenticationProvider extends AbstractUserDetailsAuthentic throw notFound; } else { try { - //其他方式登录:查询账号 没有->创建账号 - //第三方登录 - if (consumerToken != null && StrUtil.isNotEmpty(consumerToken.getOpenid())) { - ApiVO apiVO = accountView.checkOpenId(consumerToken); - if (apiVO.isSucc()) { - //已有账号 - User consumer = apiVO.getObject1(User.class); - // 处理用户权限 - List 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()); - } - } + third(consumerToken, loadedUser, logtype); } catch (Exception e) { e.printStackTrace(); if (e instanceof ThirdPartyAuthorizationException) { @@ -229,6 +199,69 @@ public class ConsumerAuthenticationProvider extends AbstractUserDetailsAuthentic 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 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() { if (this.userNotFoundEncodedPassword == null) { this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD); @@ -249,7 +282,7 @@ public class ConsumerAuthenticationProvider extends AbstractUserDetailsAuthentic * @param consumer 实际用户信息 * @return response User */ - private UserDetails display(ConsumerToken consumerToken, User consumer) { + protected UserDetails display(ConsumerToken consumerToken, User consumer) { // 处理用户权限 List authorities = new ArrayList<>(); for (String role : consumer.getRoles()) { diff --git a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/SecurityConfig.java b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/SecurityConfig.java index 477f29a..bbede7b 100644 --- a/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/SecurityConfig.java +++ b/yexuejc-springboot-base/src/main/java/com/yexuejc/springboot/base/security/SecurityConfig.java @@ -82,13 +82,22 @@ public abstract class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public ConsumerAuthenticationProcessingFilter consumerAuthenticationProcessingFilter( AuthenticationManager authenticationManager) throws Exception { - ConsumerAuthenticationProcessingFilter filter = new ConsumerAuthenticationProcessingFilter - (authenticationManager); + ConsumerAuthenticationProcessingFilter filter = createConsumerAuthenticationProcessingFilter(authenticationManager); filter.setAuthenticationManager(this.authenticationManager()); loginHodler(filter); return filter; } + /** + * 初始化 ConsumerAuthenticationProcessingFilter + * + * @param authenticationManager + * @return + */ + protected ConsumerAuthenticationProcessingFilter createConsumerAuthenticationProcessingFilter(AuthenticationManager authenticationManager) { + return new ConsumerAuthenticationProcessingFilter(authenticationManager); + } + /** *
      * 处理登录
@@ -103,8 +112,7 @@ public abstract class SecurityConfig extends WebSecurityConfigurerAdapter {
 
     @Bean
     public LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint() {
-        LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint
-                ("/login");
+        LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint("/login");
         return loginUrlAuthenticationEntryPoint;
     }