From d0bf51d75d63cf6e27090c6dd3a4f686b38a756f Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Sun, 25 Jul 2021 21:24:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E7=94=A8=E6=88=B7=E7=B1=BB=E5=9E=8B=E6=89=A9?= =?UTF-8?q?=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....java => AuthClientAutoConfiguration.java} | 11 +- .../oauth2/common/store/AuthDbTokenStore.java | 2 + .../common/store/AuthJwtTokenStore.java | 2 + .../common/store/AuthRedisTokenStore.java | 2 + .../oauth2/common/store/ResJwtTokenStore.java | 2 + .../token/CustomWebAuthenticationDetails.java | 37 +++++ .../central/oauth2/common/util/AuthUtils.java | 22 ++- .../main/resources/META-INF/spring.factories | 6 +- .../common/constant/SecurityConstants.java | 18 +++ .../resolver/TokenArgumentResolver.java | 5 + .../auth/Oauth2AuthSuccessHandler.java | 7 +- .../filter/pre/UserInfoHeaderFilter.java | 6 + .../config/AuthorizationServerConfig.java | 14 +- .../central/oauth/config/SecurityConfig.java | 27 +++- .../oauth/config/TokenGranterConfig.java | 17 +- .../oauth/filter/OauthTokenAspect.java | 5 +- .../mobile/MobileAuthenticationProvider.java | 14 +- .../MobileAuthenticationSecurityConfig.java | 13 +- .../CustomAuthenticationDetailsSource.java | 31 ++++ .../java/com/central/oauth/model/TokenVo.java | 7 + .../openid/OpenIdAuthenticationProvider.java | 22 ++- .../OpenIdAuthenticationSecurityConfig.java | 9 +- .../PasswordAuthenticationProvider.java | 148 ++++++++++++++++++ .../oauth/service/ZltUserDetailsService.java | 54 ++++--- .../service/impl/CustomTokenServices.java | 113 +++++++++++++ .../service/impl/RedisTokensServiceImpl.java | 4 + .../impl/UserDetailServiceFactory.java | 53 +++++++ .../service/impl/UserDetailServiceImpl.java | 118 +++++++------- ...serDetailsByNameServiceFactoryWrapper.java | 59 +++++++ .../oauth/tenant/OauthAuthorizeAspect.java | 15 +- .../tenant/TenantAuthenticationProvider.java | 4 +- .../TenantAuthenticationSecurityConfig.java | 12 +- .../src/main/resources/static/index.html | 2 +- .../resources/static/pages/system/tokens.html | 3 +- 34 files changed, 720 insertions(+), 144 deletions(-) rename zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/{config/SecurityPropertiesConfig.java => AuthClientAutoConfiguration.java} (65%) create mode 100644 zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/CustomWebAuthenticationDetails.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/model/CustomAuthenticationDetailsSource.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/password/PasswordAuthenticationProvider.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceFactory.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailsByNameServiceFactoryWrapper.java diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/SecurityPropertiesConfig.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/AuthClientAutoConfiguration.java similarity index 65% rename from zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/SecurityPropertiesConfig.java rename to zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/AuthClientAutoConfiguration.java index e579e444..17ede133 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/SecurityPropertiesConfig.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/AuthClientAutoConfiguration.java @@ -1,16 +1,21 @@ -package com.central.oauth2.common.config; +package com.central.oauth2.common; import com.central.oauth2.common.properties.SecurityProperties; import com.central.oauth2.common.properties.TokenStoreProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.ComponentScan; /** + * 鉴权自动配置 + * * @author zlt - * @date 2019/10/7 + * @version 1.0 + * @date 2021/7/24 *

* Blog: https://zlt2000.gitee.io * Github: https://github.com/zlt2000 */ @EnableConfigurationProperties({SecurityProperties.class, TokenStoreProperties.class}) -public class SecurityPropertiesConfig { +@ComponentScan +public class AuthClientAutoConfiguration { } diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthDbTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthDbTokenStore.java index d084e549..1d0b1ec9 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthDbTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthDbTokenStore.java @@ -3,6 +3,7 @@ package com.central.oauth2.common.store; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; @@ -14,6 +15,7 @@ import javax.sql.DataSource; * @author zlt * @date 2018/7/24 16:23 */ +@Configuration @ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "db") public class AuthDbTokenStore { @Autowired diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java index e23b3d41..2d261222 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java @@ -5,6 +5,7 @@ import com.central.oauth2.common.converter.CustomUserAuthenticationConverter; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.bootstrap.encrypt.KeyProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; @@ -28,6 +29,7 @@ import java.util.Map; * Blog: https://zlt2000.gitee.io * Github: https://github.com/zlt2000 */ +@Configuration @ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "authJwt") public class AuthJwtTokenStore { @Resource diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java index 8d5094c3..239dc420 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java @@ -4,6 +4,7 @@ import com.central.oauth2.common.properties.SecurityProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.security.oauth2.provider.token.TokenStore; @@ -18,6 +19,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore; * Blog: https://zlt2000.gitee.io * Github: https://github.com/zlt2000 */ +@Configuration @ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true) public class AuthRedisTokenStore { @Bean diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java index 606fbb68..c7655696 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java @@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; @@ -31,6 +32,7 @@ import java.util.stream.Collectors; * @author zlt * @date 2018/8/20 9:25 */ +@Configuration @ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "resJwt") public class ResJwtTokenStore { @Autowired diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/CustomWebAuthenticationDetails.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/CustomWebAuthenticationDetails.java new file mode 100644 index 00000000..95dd1c18 --- /dev/null +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/CustomWebAuthenticationDetails.java @@ -0,0 +1,37 @@ +package com.central.oauth2.common.token; + +import lombok.Getter; + +import java.io.Serializable; + +/** + * 表单登录的认证信息对象 + * + * @author zlt + * @version 1.0 + * @date 2021/7/24 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Getter +public class CustomWebAuthenticationDetails implements Serializable { + private static final long serialVersionUID = - 1; + + private final String accountType; + private final String remoteAddress; + private final String sessionId; + + public CustomWebAuthenticationDetails(String remoteAddress, String sessionId, String accountType) { + this.remoteAddress = remoteAddress; + this.sessionId = sessionId; + this.accountType = accountType; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(super.toString()).append("; accountType: ").append(this.getAccountType()); + return sb.toString(); + } +} diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java index bad4669f..4b717617 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java @@ -1,17 +1,19 @@ package com.central.oauth2.common.util; import com.central.common.constant.CommonConstant; +import com.central.common.constant.SecurityConstants; import com.central.common.model.SysUser; +import com.central.oauth2.common.token.CustomWebAuthenticationDetails; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.Enumeration; +import java.util.Map; /** * 认证授权相关工具类 @@ -104,4 +106,22 @@ public class AuthUtils { } return username; } + + /** + * 获取登陆的帐户类型 + */ + public static String getAccountType(Authentication authentication) { + Object details = authentication.getDetails(); + String accountType = null; + if (details instanceof CustomWebAuthenticationDetails) { + CustomWebAuthenticationDetails detailsObj = (CustomWebAuthenticationDetails) details; + accountType = detailsObj.getAccountType(); + } else { + Map detailsMap = (Map) details; + if (detailsMap != null) { + accountType = detailsMap.get(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME); + } + } + return accountType; + } } diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/resources/META-INF/spring.factories index f45f3e08..9862b1d3 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,6 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.central.oauth2.common.config.SecurityPropertiesConfig,\ -com.central.oauth2.common.store.AuthDbTokenStore,\ -com.central.oauth2.common.store.AuthRedisTokenStore,\ -com.central.oauth2.common.store.AuthJwtTokenStore,\ -com.central.oauth2.common.store.ResJwtTokenStore \ No newline at end of file +com.central.oauth2.common.AuthClientAutoConfiguration \ No newline at end of file diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/SecurityConstants.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/SecurityConstants.java index 01526326..45f0c129 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/SecurityConstants.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/constant/SecurityConstants.java @@ -4,6 +4,9 @@ package com.central.common.constant; * Security 权限常量 * * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ public interface SecurityConstants { /** @@ -31,6 +34,11 @@ public interface SecurityConstants { */ String TENANT_HEADER = "x-tenant-header"; + /** + * 账号类型信息头 + */ + String ACCOUNT_TYPE_HEADER = "x-account-type-header"; + /** * 基础角色 */ @@ -167,4 +175,14 @@ public interface SecurityConstants { * 令牌颁发者 */ String ISS = "http://zlt2000.cn"; + + /** + * 默认账号类型 + */ + String DEF_ACCOUNT_TYPE = "admin"; + + /** + * 账号类型参数名 + */ + String ACCOUNT_TYPE_PARAM_NAME = "account_type"; } diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/resolver/TokenArgumentResolver.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/resolver/TokenArgumentResolver.java index 19a1183f..32e5ef37 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/resolver/TokenArgumentResolver.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/resolver/TokenArgumentResolver.java @@ -23,6 +23,9 @@ import java.util.List; * * @author zlt * @date 2018/12/21 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Slf4j public class TokenArgumentResolver implements HandlerMethodArgumentResolver { @@ -61,6 +64,8 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver { String userId = request.getHeader(SecurityConstants.USER_ID_HEADER); String username = request.getHeader(SecurityConstants.USER_HEADER); String roles = request.getHeader(SecurityConstants.ROLE_HEADER); + //账号类型 + String accountType = request.getHeader(SecurityConstants.ACCOUNT_TYPE_HEADER); if (StrUtil.isBlank(username)) { log.warn("resolveArgument error username is empty"); return null; diff --git a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/Oauth2AuthSuccessHandler.java b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/Oauth2AuthSuccessHandler.java index dd637076..8a02c897 100644 --- a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/Oauth2AuthSuccessHandler.java +++ b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/Oauth2AuthSuccessHandler.java @@ -3,6 +3,7 @@ package com.central.gateway.auth; import cn.hutool.core.collection.CollectionUtil; import com.central.common.constant.SecurityConstants; import com.central.common.model.SysUser; +import com.central.oauth2.common.util.AuthUtils; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication; @@ -37,12 +38,12 @@ public class Oauth2AuthSuccessHandler implements ServerAuthenticationSuccessHand String clientId = oauth2Authentication.getOAuth2Request().getClientId(); headerValues.add(SecurityConstants.TENANT_HEADER, clientId); headerValues.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); + String accountType = AuthUtils.getAccountType(oauth2Authentication.getUserAuthentication()); + headerValues.add(SecurityConstants.ACCOUNT_TYPE_HEADER, accountType); ServerWebExchange exchange = webFilterExchange.getExchange(); ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate() - .headers(h -> { - h.addAll(headerValues); - }) + .headers(h -> h.addAll(headerValues)) .build(); ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build(); diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java index ae832a0d..bee07e26 100644 --- a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java +++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java @@ -3,6 +3,7 @@ package com.central.gateway.filter.pre; import cn.hutool.core.collection.CollectionUtil; import com.central.common.constant.SecurityConstants; import com.central.common.model.SysUser; +import com.central.oauth2.common.util.AuthUtils; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; @@ -19,6 +20,9 @@ import static org.springframework.cloud.netflix.zuul.filters.support.FilterConst * * @author zlt * @date 2018/11/20 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Component public class UserInfoHeaderFilter extends ZuulFilter { @@ -53,6 +57,8 @@ public class UserInfoHeaderFilter extends ZuulFilter { String clientId = oauth2Authentication.getOAuth2Request().getClientId(); ctx.addZuulRequestHeader(SecurityConstants.TENANT_HEADER, clientId); ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); + String accountType = AuthUtils.getAccountType(oauth2Authentication.getUserAuthentication()); + ctx.addZuulRequestHeader(SecurityConstants.ACCOUNT_TYPE_HEADER, accountType); } return null; } diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/AuthorizationServerConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/AuthorizationServerConfig.java index d9f8c2a6..85dd7950 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/config/AuthorizationServerConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/config/AuthorizationServerConfig.java @@ -5,9 +5,11 @@ import com.central.common.model.SysUser; import com.central.oauth.model.Client; import com.central.oauth.service.IClientService; import com.central.oauth.service.impl.RedisClientDetailsService; +import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth.utils.OidcIdTokenBuilder; import com.central.oauth2.common.constants.IdTokenClaimNames; import com.central.oauth2.common.properties.TokenStoreProperties; +import com.central.oauth2.common.util.AuthUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.cloud.bootstrap.encrypt.KeyProperties; @@ -15,7 +17,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; @@ -54,7 +55,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap private AuthenticationManager authenticationManager; @Resource - private UserDetailsService userDetailsService; + private UserDetailServiceFactory userDetailsServiceFactory; @Autowired private TokenStore tokenStore; @@ -79,7 +80,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints.tokenStore(tokenStore) .authenticationManager(authenticationManager) - .userDetailsService(userDetailsService) + //.userDetailsService(userDetailsServiceFactory.getService(SecurityConstants.DEF_ACCOUNT_TYPE)) .authorizationCodeServices(authorizationCodeServices) .exceptionTranslator(webResponseExceptionTranslator) .tokenGranter(tokenGranter); @@ -117,9 +118,12 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap , TokenStoreProperties tokenStoreProperties) { return (accessToken, authentication) -> { Set responseTypes = authentication.getOAuth2Request().getResponseTypes(); + Map additionalInfo = new HashMap<>(3); + String accountType = AuthUtils.getAccountType(authentication.getUserAuthentication()); + additionalInfo.put(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME, accountType); + if (responseTypes.contains(SecurityConstants.ID_TOKEN) || "authJwt".equals(tokenStoreProperties.getType())) { - Map additionalInfo = new HashMap<>(2); Object principal = authentication.getPrincipal(); //增加id参数 if (principal instanceof SysUser) { @@ -132,8 +136,8 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap additionalInfo.put("id", user.getId()); } } - ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); } + ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); return accessToken; }; } diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java index aa308742..a1328973 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java @@ -4,21 +4,24 @@ import com.central.common.constant.SecurityConstants; import com.central.common.properties.TenantProperties; import com.central.oauth.filter.LoginProcessSetTenantFilter; import com.central.oauth.handler.OauthLogoutSuccessHandler; +import com.central.oauth.password.PasswordAuthenticationProvider; +import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth.tenant.TenantAuthenticationSecurityConfig; import com.central.oauth.tenant.TenantUsernamePasswordAuthenticationFilter; import com.central.oauth.mobile.MobileAuthenticationSecurityConfig; import com.central.oauth.openid.OpenIdAuthenticationSecurityConfig; import com.central.common.config.DefaultPasswordConfig; +import com.central.oauth2.common.token.CustomWebAuthenticationDetails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.security.authentication.AuthenticationDetailsSource; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.http.SessionCreationPolicy; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; @@ -27,6 +30,7 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic import org.springframework.security.web.authentication.logout.LogoutHandler; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; /** * spring security配置 @@ -48,7 +52,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { private AuthenticationEntryPoint authenticationEntryPoint; @Resource - private UserDetailsService userDetailsService; + private UserDetailServiceFactory userDetailsServiceFactory; @Autowired private PasswordEncoder passwordEncoder; @@ -71,6 +75,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private TenantProperties tenantProperties; + @Autowired + private AuthenticationDetailsSource authenticationDetailsSource; + /** * 这一步的配置是必不可少的,否则SpringBoot会自动配置一个AuthenticationManager,覆盖掉内存中的用户 * @return 认证管理对象 @@ -88,6 +95,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { filter.setFilterProcessesUrl(SecurityConstants.OAUTH_LOGIN_PRO_URL); filter.setAuthenticationSuccessHandler(authenticationSuccessHandler); filter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler(SecurityConstants.LOGIN_FAILURE_PAGE)); + filter.setAuthenticationDetailsSource(authenticationDetailsSource); return filter; } @@ -124,7 +132,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.formLogin() .loginPage(SecurityConstants.LOGIN_PAGE) .loginProcessingUrl(SecurityConstants.OAUTH_LOGIN_PRO_URL) - .successHandler(authenticationSuccessHandler); + .successHandler(authenticationSuccessHandler) + .authenticationDetailsSource(authenticationDetailsSource); } @@ -141,8 +150,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { /** * 全局用户信息 */ - @Autowired - public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception { - auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); + @Override + public void configure(AuthenticationManagerBuilder auth) { + PasswordAuthenticationProvider provider = new PasswordAuthenticationProvider(); + provider.setPasswordEncoder(passwordEncoder); + provider.setUserDetailsServiceFactory(userDetailsServiceFactory); + auth.authenticationProvider(provider); } + /*public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); + }*/ } diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java index b6adc821..514d70e7 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java @@ -5,14 +5,14 @@ import com.central.oauth.granter.OpenIdGranter; import com.central.oauth.granter.PwdImgCodeGranter; import com.central.oauth.service.IValidateCodeService; import com.central.oauth.service.impl.CustomTokenServices; +import com.central.oauth.service.impl.UserDetailServiceFactory; +import com.central.oauth.service.impl.UserDetailsByNameServiceFactoryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.ProviderManager; -import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.provider.*; import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter; @@ -27,6 +27,7 @@ import org.springframework.security.oauth2.provider.request.DefaultOAuth2Request import org.springframework.security.oauth2.provider.token.*; import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -45,8 +46,8 @@ public class TokenGranterConfig { @Autowired private ClientDetailsService clientDetailsService; - @Autowired - private UserDetailsService userDetailsService; + @Resource + private UserDetailServiceFactory userDetailsServiceFactory; @Autowired private AuthenticationManager authenticationManager; @@ -163,7 +164,7 @@ public class TokenGranterConfig { tokenServices.setReuseRefreshToken(reuseRefreshToken); tokenServices.setClientDetailsService(clientDetailsService); tokenServices.setTokenEnhancer(tokenEnhancer()); - addUserDetailsService(tokenServices, this.userDetailsService); + addUserDetailsService(tokenServices); return tokenServices; } @@ -176,10 +177,10 @@ public class TokenGranterConfig { return null; } - private void addUserDetailsService(DefaultTokenServices tokenServices, UserDetailsService userDetailsService) { - if (userDetailsService != null) { + private void addUserDetailsService(DefaultTokenServices tokenServices) { + if (this.userDetailsServiceFactory != null) { PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider(); - provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(userDetailsService)); + provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceFactoryWrapper<>(this.userDetailsServiceFactory)); tokenServices.setAuthenticationManager(new ProviderManager(Collections.singletonList(provider))); } } diff --git a/zlt-uaa/src/main/java/com/central/oauth/filter/OauthTokenAspect.java b/zlt-uaa/src/main/java/com/central/oauth/filter/OauthTokenAspect.java index 2468b66b..7e101e6f 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/filter/OauthTokenAspect.java +++ b/zlt-uaa/src/main/java/com/central/oauth/filter/OauthTokenAspect.java @@ -46,10 +46,13 @@ public class OauthTokenAspect { String clientId = getClientId(principal); Map parameters = (Map) args[1]; String grantType = parameters.get(OAuth2Utils.GRANT_TYPE); + if (!parameters.containsKey(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME)) { + parameters.put(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME, SecurityConstants.DEF_ACCOUNT_TYPE); + } //保存租户id TenantContextHolder.setTenant(clientId); - Object proceed = joinPoint.proceed(); + Object proceed = joinPoint.proceed(args); if (SecurityConstants.AUTHORIZATION_CODE.equals(grantType)) { /* 如果使用 @EnableOAuth2Sso 注解不能修改返回格式,否则授权码模式可以统一改 diff --git a/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationProvider.java b/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationProvider.java index 0d2fc1f3..99e5fcde 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationProvider.java +++ b/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationProvider.java @@ -1,10 +1,10 @@ package com.central.oauth.mobile; -import com.central.oauth.service.ZltUserDetailsService; +import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth2.common.token.MobileAuthenticationToken; +import lombok.Getter; import lombok.Setter; import org.springframework.security.authentication.AuthenticationProvider; -import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.InternalAuthenticationServiceException; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; @@ -12,10 +12,14 @@ import org.springframework.security.crypto.password.PasswordEncoder; /** * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Setter +@Getter public class MobileAuthenticationProvider implements AuthenticationProvider { - private ZltUserDetailsService userDetailsService; + private UserDetailServiceFactory userDetailsServiceFactory; private PasswordEncoder passwordEncoder; @Override @@ -23,12 +27,12 @@ public class MobileAuthenticationProvider implements AuthenticationProvider { MobileAuthenticationToken authenticationToken = (MobileAuthenticationToken) authentication; String mobile = (String) authenticationToken.getPrincipal(); String password = (String) authenticationToken.getCredentials(); - UserDetails user = userDetailsService.loadUserByMobile(mobile); + UserDetails user = userDetailsServiceFactory.getService(authenticationToken).loadUserByMobile(mobile); if (user == null) { throw new InternalAuthenticationServiceException("手机号或密码错误"); } if (!passwordEncoder.matches(password, user.getPassword())) { - throw new BadCredentialsException("手机号或密码错误"); + throw new InternalAuthenticationServiceException("手机号或密码错误"); } MobileAuthenticationToken authenticationResult = new MobileAuthenticationToken(user, password, user.getAuthorities()); authenticationResult.setDetails(authenticationToken.getDetails()); diff --git a/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationSecurityConfig.java b/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationSecurityConfig.java index 12d09abc..4aaa7a9a 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationSecurityConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/mobile/MobileAuthenticationSecurityConfig.java @@ -1,6 +1,6 @@ package com.central.oauth.mobile; -import com.central.oauth.service.ZltUserDetailsService; +import com.central.oauth.service.impl.UserDetailServiceFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.SecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -8,15 +8,20 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.DefaultSecurityFilterChain; import org.springframework.stereotype.Component; +import javax.annotation.Resource; + /** * mobile的相关处理配置 * * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Component public class MobileAuthenticationSecurityConfig extends SecurityConfigurerAdapter { - @Autowired - private ZltUserDetailsService userDetailsService; + @Resource + private UserDetailServiceFactory userDetailsServiceFactory; @Autowired private PasswordEncoder passwordEncoder; @@ -25,7 +30,7 @@ public class MobileAuthenticationSecurityConfig extends SecurityConfigurerAdapte public void configure(HttpSecurity http) { //mobile provider MobileAuthenticationProvider provider = new MobileAuthenticationProvider(); - provider.setUserDetailsService(userDetailsService); + provider.setUserDetailsServiceFactory(userDetailsServiceFactory); provider.setPasswordEncoder(passwordEncoder); http.authenticationProvider(provider); } diff --git a/zlt-uaa/src/main/java/com/central/oauth/model/CustomAuthenticationDetailsSource.java b/zlt-uaa/src/main/java/com/central/oauth/model/CustomAuthenticationDetailsSource.java new file mode 100644 index 00000000..77e36ea4 --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/model/CustomAuthenticationDetailsSource.java @@ -0,0 +1,31 @@ +package com.central.oauth.model; + +import com.central.common.constant.SecurityConstants; +import com.central.oauth2.common.token.CustomWebAuthenticationDetails; +import org.springframework.security.authentication.AuthenticationDetailsSource; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +/** + * 表单登录的认证信息对象 + * + * @author zlt + * @version 1.0 + * @date 2021/7/21 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Component +public class CustomAuthenticationDetailsSource implements AuthenticationDetailsSource { + @Override + public CustomWebAuthenticationDetails buildDetails(HttpServletRequest context) { + String remoteAddress = context.getRemoteAddr(); + HttpSession session = context.getSession(false); + String sessionId = session != null ? session.getId() : null; + String accountType = context.getParameter(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME); + return new CustomWebAuthenticationDetails(remoteAddress, sessionId, accountType); + } +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java b/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java index 3b0ccdd3..2b107138 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java +++ b/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java @@ -8,6 +8,9 @@ import java.util.Date; /** * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Setter @Getter @@ -33,4 +36,8 @@ public class TokenVo implements Serializable { * 授权类型 */ private String grantType; + /** + * 账号类型 + */ + private String accountType; } diff --git a/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationProvider.java b/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationProvider.java index 759124ba..4a4623d8 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationProvider.java +++ b/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationProvider.java @@ -1,24 +1,30 @@ package com.central.oauth.openid; +import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth2.common.token.OpenIdAuthenticationToken; +import lombok.Getter; +import lombok.Setter; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.InternalAuthenticationServiceException; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.social.security.SocialUserDetailsService; /** * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ +@Setter +@Getter public class OpenIdAuthenticationProvider implements AuthenticationProvider { - - private SocialUserDetailsService userDetailsService; + private UserDetailServiceFactory userDetailsServiceFactory; @Override public Authentication authenticate(Authentication authentication) { OpenIdAuthenticationToken authenticationToken = (OpenIdAuthenticationToken) authentication; String openId = (String) authenticationToken.getPrincipal(); - UserDetails user = userDetailsService.loadUserByUserId(openId); + UserDetails user = userDetailsServiceFactory.getService(authenticationToken).loadUserByUserId(openId); if (user == null) { throw new InternalAuthenticationServiceException("openId错误"); } @@ -31,12 +37,4 @@ public class OpenIdAuthenticationProvider implements AuthenticationProvider { public boolean supports(Class authentication) { return OpenIdAuthenticationToken.class.isAssignableFrom(authentication); } - - public SocialUserDetailsService getUserDetailsService() { - return userDetailsService; - } - - public void setUserDetailsService(SocialUserDetailsService userDetailsService) { - this.userDetailsService = userDetailsService; - } } diff --git a/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationSecurityConfig.java b/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationSecurityConfig.java index 2a63c0b2..f5ef56cb 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationSecurityConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/openid/OpenIdAuthenticationSecurityConfig.java @@ -1,27 +1,30 @@ package com.central.oauth.openid; +import com.central.oauth.service.impl.UserDetailServiceFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.SecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.DefaultSecurityFilterChain; -import org.springframework.social.security.SocialUserDetailsService; import org.springframework.stereotype.Component; /** * openId的相关处理配置 * * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Component public class OpenIdAuthenticationSecurityConfig extends SecurityConfigurerAdapter { @Autowired - private SocialUserDetailsService userDetailsService; + private UserDetailServiceFactory userDetailsServiceFactory; @Override public void configure(HttpSecurity http) { //openId provider OpenIdAuthenticationProvider provider = new OpenIdAuthenticationProvider(); - provider.setUserDetailsService(userDetailsService); + provider.setUserDetailsServiceFactory(userDetailsServiceFactory); http.authenticationProvider(provider); } } diff --git a/zlt-uaa/src/main/java/com/central/oauth/password/PasswordAuthenticationProvider.java b/zlt-uaa/src/main/java/com/central/oauth/password/PasswordAuthenticationProvider.java new file mode 100644 index 00000000..7fbdd579 --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/password/PasswordAuthenticationProvider.java @@ -0,0 +1,148 @@ +package com.central.oauth.password; + +import com.central.oauth.service.impl.UserDetailServiceFactory; +import lombok.Getter; +import lombok.Setter; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.InternalAuthenticationServiceException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsPasswordService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.factory.PasswordEncoderFactories; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.util.Assert; + +/** + * 扩展用户名密码provider + * + * @author zlt + * @version 1.0 + * @date 2021/7/24 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Setter +@Getter +public class PasswordAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { + private UserDetailServiceFactory userDetailsServiceFactory; + + private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword"; + + private PasswordEncoder passwordEncoder; + + /** + * The password used to perform + * {@link PasswordEncoder#matches(CharSequence, String)} on when the user is + * not found to avoid SEC-2056. This is necessary, because some + * {@link PasswordEncoder} implementations will short circuit if the password is not + * in a valid format. + */ + private volatile String userNotFoundEncodedPassword; + + private UserDetailsPasswordService userDetailsPasswordService; + + public PasswordAuthenticationProvider() { + setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder()); + } + + // ~ Methods + // ======================================================================================================== + + @Override + @SuppressWarnings("deprecation") + protected void additionalAuthenticationChecks(UserDetails userDetails, + UsernamePasswordAuthenticationToken authentication) + throws AuthenticationException { + if (authentication.getCredentials() == null) { + logger.debug("Authentication failed: no credentials provided"); + + throw new BadCredentialsException(messages.getMessage( + "AbstractUserDetailsAuthenticationProvider.badCredentials", + "Bad credentials")); + } + + String presentedPassword = authentication.getCredentials().toString(); + + if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) { + logger.debug("Authentication failed: password does not match stored value"); + + throw new BadCredentialsException(messages.getMessage( + "AbstractUserDetailsAuthenticationProvider.badCredentials", + "Bad credentials")); + } + } + + @Override + protected void doAfterPropertiesSet() { + Assert.notNull(this.userDetailsServiceFactory, "A UserDetailsService must be set"); + } + + @Override + protected final UserDetails retrieveUser(String username, + UsernamePasswordAuthenticationToken authentication) + throws AuthenticationException { + prepareTimingAttackProtection(); + try { + UserDetails loadedUser = userDetailsServiceFactory.getService(authentication).loadUserByUsername(username); + if (loadedUser == null) { + throw new InternalAuthenticationServiceException( + "UserDetailsService returned null, which is an interface contract violation"); + } + return loadedUser; + } + catch (UsernameNotFoundException ex) { + mitigateAgainstTimingAttack(authentication); + throw ex; + } + catch (InternalAuthenticationServiceException ex) { + throw ex; + } + catch (Exception ex) { + throw new InternalAuthenticationServiceException(ex.getMessage(), ex); + } + } + + @Override + protected Authentication createSuccessAuthentication(Object principal, + Authentication authentication, UserDetails user) { + boolean upgradeEncoding = this.userDetailsPasswordService != null + && this.passwordEncoder.upgradeEncoding(user.getPassword()); + if (upgradeEncoding) { + String presentedPassword = authentication.getCredentials().toString(); + String newPassword = this.passwordEncoder.encode(presentedPassword); + user = this.userDetailsPasswordService.updatePassword(user, newPassword); + } + return super.createSuccessAuthentication(principal, authentication, user); + } + + private void prepareTimingAttackProtection() { + if (this.userNotFoundEncodedPassword == null) { + this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD); + } + } + + private void mitigateAgainstTimingAttack(UsernamePasswordAuthenticationToken authentication) { + if (authentication.getCredentials() != null) { + String presentedPassword = authentication.getCredentials().toString(); + this.passwordEncoder.matches(presentedPassword, this.userNotFoundEncodedPassword); + } + } + + /** + * Sets the PasswordEncoder instance to be used to encode and validate passwords. If + * not set, the password will be compared using {@link PasswordEncoderFactories#createDelegatingPasswordEncoder()} + * + * @param passwordEncoder must be an instance of one of the {@code PasswordEncoder} + * types. + */ + public void setPasswordEncoder(PasswordEncoder passwordEncoder) { + Assert.notNull(passwordEncoder, "passwordEncoder cannot be null"); + this.passwordEncoder = passwordEncoder; + this.userNotFoundEncodedPassword = null; + } +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java b/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java index 5fbb2942..c02809d1 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java @@ -1,19 +1,35 @@ -package com.central.oauth.service; - -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; - -/** - * @author zlt - * @date 2018/12/28 - */ -public interface ZltUserDetailsService extends UserDetailsService { - /** - * 根据电话号码查询用户 - * - * @param mobile - * @return - */ - UserDetails loadUserByMobile(String mobile); -} +package com.central.oauth.service; + +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.social.security.SocialUserDetails; + +/** + * @author zlt + * @date 2018/12/28 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +public interface ZltUserDetailsService extends UserDetailsService { + /** + * 判断实现类是否属于该类型 + * @param accountType 账号类型 + */ + boolean supports(String accountType); + + /** + * 根据电话号码查询用户 + * + * @param mobile + * @return + */ + UserDetails loadUserByMobile(String mobile); + + /** + * 根据用户id/openId查询用户 + * @param userId 用户id/openId + */ + SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException; +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java index 4f9f46ee..66e1c624 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java @@ -1,12 +1,21 @@ package com.central.oauth.service.impl; import com.central.common.constant.SecurityConstants; +import org.springframework.security.authentication.AbstractAuthenticationToken; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.oauth2.common.*; +import org.springframework.security.oauth2.common.exceptions.InvalidGrantException; +import org.springframework.security.oauth2.common.exceptions.InvalidScopeException; +import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.OAuth2Request; +import org.springframework.security.oauth2.provider.TokenRequest; import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenStore; +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; import org.springframework.transaction.annotation.Transactional; import java.util.Date; @@ -25,6 +34,9 @@ import java.util.UUID; public class CustomTokenServices extends DefaultTokenServices { private TokenStore tokenStore; private TokenEnhancer accessTokenEnhancer; + private AuthenticationManager authenticationManager; + private boolean supportRefreshToken = false; + private boolean reuseRefreshToken = true; /** * 是否登录同应用同账号互踢 @@ -131,6 +143,79 @@ public class CustomTokenServices extends DefaultTokenServices { return accessTokenEnhancer != null ? accessTokenEnhancer.enhance(token, authentication) : token; } + @Override + @Transactional(noRollbackFor={InvalidTokenException.class, InvalidGrantException.class}) + public OAuth2AccessToken refreshAccessToken(String refreshTokenValue, TokenRequest tokenRequest) + throws AuthenticationException { + if (!supportRefreshToken) { + throw new InvalidGrantException("Invalid refresh token: " + refreshTokenValue); + } + + OAuth2RefreshToken refreshToken = tokenStore.readRefreshToken(refreshTokenValue); + if (refreshToken == null) { + throw new InvalidGrantException("Invalid refresh token: " + refreshTokenValue); + } + + OAuth2Authentication authentication = tokenStore.readAuthenticationForRefreshToken(refreshToken); + if (this.authenticationManager != null && !authentication.isClientOnly()) { + AbstractAuthenticationToken userAuthentication = (AbstractAuthenticationToken)authentication.getUserAuthentication(); + Object usesrDetails = userAuthentication.getDetails(); + // The client has already been authenticated, but the user authentication might be old now, so give it a + // chance to re-authenticate. + Authentication user = new PreAuthenticatedAuthenticationToken(userAuthentication, "", authentication.getAuthorities()); + user = authenticationManager.authenticate(user); + //保存账号类型 + ((PreAuthenticatedAuthenticationToken)user).setDetails(usesrDetails); + Object details = authentication.getDetails(); + authentication = new OAuth2Authentication(authentication.getOAuth2Request(), user); + authentication.setDetails(details); + } + String clientId = authentication.getOAuth2Request().getClientId(); + if (clientId == null || !clientId.equals(tokenRequest.getClientId())) { + throw new InvalidGrantException("Wrong client for this refresh token: " + refreshTokenValue); + } + + // clear out any access tokens already associated with the refresh + // token. + tokenStore.removeAccessTokenUsingRefreshToken(refreshToken); + + if (isExpired(refreshToken)) { + tokenStore.removeRefreshToken(refreshToken); + throw new InvalidTokenException("Invalid refresh token (expired): " + refreshToken); + } + + authentication = createRefreshedAuthentication(authentication, tokenRequest); + if (!reuseRefreshToken) { + tokenStore.removeRefreshToken(refreshToken); + refreshToken = createRefreshToken(authentication); + } + + OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken); + tokenStore.storeAccessToken(accessToken, authentication); + if (!reuseRefreshToken) { + tokenStore.storeRefreshToken(accessToken.getRefreshToken(), authentication); + } + return accessToken; + } + + private OAuth2Authentication createRefreshedAuthentication(OAuth2Authentication authentication, TokenRequest request) { + OAuth2Authentication narrowed; + Set scope = request.getScope(); + OAuth2Request clientAuth = authentication.getOAuth2Request().refresh(request); + if (scope != null && !scope.isEmpty()) { + Set originalScope = clientAuth.getScope(); + if (originalScope == null || !originalScope.containsAll(scope)) { + throw new InvalidScopeException("Unable to narrow the scope of the client authentication to " + scope + + ".", originalScope); + } + else { + clientAuth = clientAuth.narrowScope(scope); + } + } + narrowed = new OAuth2Authentication(clientAuth, authentication.getUserAuthentication()); + return narrowed; + } + @Override public void setTokenStore(TokenStore tokenStore) { this.tokenStore = tokenStore; @@ -142,4 +227,32 @@ public class CustomTokenServices extends DefaultTokenServices { this.accessTokenEnhancer = accessTokenEnhancer; super.setTokenEnhancer(accessTokenEnhancer); } + + @Override + public void setAuthenticationManager(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + super.setAuthenticationManager(authenticationManager); + } + + /** + * Whether to support the refresh token. + * + * @param supportRefreshToken Whether to support the refresh token. + */ + @Override + public void setSupportRefreshToken(boolean supportRefreshToken) { + this.supportRefreshToken = supportRefreshToken; + super.setSupportRefreshToken(supportRefreshToken); + } + + /** + * Whether to reuse refresh tokens (until expired). + * + * @param reuseRefreshToken Whether to reuse refresh tokens (until expired). + */ + @Override + public void setReuseRefreshToken(boolean reuseRefreshToken) { + this.reuseRefreshToken = reuseRefreshToken; + super.setReuseRefreshToken(reuseRefreshToken); + } } diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java index 3f65b800..70189dab 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java @@ -65,6 +65,10 @@ public class RedisTokensServiceImpl implements ITokensService { tokenVo.setGrantType(request.getGrantType()); } + Map additionalInformation = accessToken.getAdditionalInformation(); + String accountType = (String)additionalInformation.get(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME); + tokenVo.setAccountType(accountType); + result.add(tokenVo); } } diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceFactory.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceFactory.java new file mode 100644 index 00000000..c8d61c3f --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceFactory.java @@ -0,0 +1,53 @@ +package com.central.oauth.service.impl; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.central.common.constant.SecurityConstants; +import com.central.oauth.service.ZltUserDetailsService; +import com.central.oauth2.common.util.AuthUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.authentication.InternalAuthenticationServiceException; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 用户service工厂 + * + * @author zlt + * @version 1.0 + * @date 2021/7/24 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Slf4j +@Service +public class UserDetailServiceFactory { + private static final String ERROR_MSG = "找不到账号类型为 {} 的实现类"; + + @Resource + private List userDetailsServices; + + public ZltUserDetailsService getService(Authentication authentication) { + String accountType = AuthUtils.getAccountType(authentication); + return this.getService(accountType); + } + + public ZltUserDetailsService getService(String accountType) { + if (StrUtil.isEmpty(accountType)) { + accountType = SecurityConstants.DEF_ACCOUNT_TYPE; + } + log.info("UserDetailServiceFactory.getService:{}", accountType); + if (CollUtil.isNotEmpty(userDetailsServices)) { + for (ZltUserDetailsService userService : userDetailsServices) { + if (userService.supports(accountType)) { + return userService; + } + } + } + throw new InternalAuthenticationServiceException(StrUtil.format(ERROR_MSG, accountType)); + } +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java index 4e18ed7b..b240fd4f 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java @@ -1,54 +1,64 @@ -package com.central.oauth.service.impl; - -import com.central.common.feign.UserService; -import com.central.oauth.service.ZltUserDetailsService; -import org.springframework.security.authentication.DisabledException; -import org.springframework.security.authentication.InternalAuthenticationServiceException; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.social.security.SocialUserDetails; -import org.springframework.social.security.SocialUserDetailsService; -import org.springframework.stereotype.Service; - -import com.central.common.model.LoginAppUser; - -import lombok.extern.slf4j.Slf4j; - -import javax.annotation.Resource; - -/** - * @author zlt - */ -@Slf4j -@Service -public class UserDetailServiceImpl implements ZltUserDetailsService, SocialUserDetailsService { - @Resource - private UserService userService; - - @Override - public UserDetails loadUserByUsername(String username) { - LoginAppUser loginAppUser = userService.findByUsername(username); - if (loginAppUser == null) { - throw new InternalAuthenticationServiceException("用户名或密码错误"); - } - return checkUser(loginAppUser); - } - - @Override - public SocialUserDetails loadUserByUserId(String openId) { - LoginAppUser loginAppUser = userService.findByOpenId(openId); - return checkUser(loginAppUser); - } - - @Override - public UserDetails loadUserByMobile(String mobile) { - LoginAppUser loginAppUser = userService.findByMobile(mobile); - return checkUser(loginAppUser); - } - - private LoginAppUser checkUser(LoginAppUser loginAppUser) { - if (loginAppUser != null && !loginAppUser.isEnabled()) { - throw new DisabledException("用户已作废"); - } - return loginAppUser; - } -} +package com.central.oauth.service.impl; + +import com.central.common.constant.SecurityConstants; +import com.central.common.feign.UserService; +import com.central.oauth.service.ZltUserDetailsService; +import org.springframework.security.authentication.DisabledException; +import org.springframework.security.authentication.InternalAuthenticationServiceException; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.social.security.SocialUserDetails; +import org.springframework.stereotype.Service; + +import com.central.common.model.LoginAppUser; + +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.Resource; + +/** + * @author zlt + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Slf4j +@Service +public class UserDetailServiceImpl implements ZltUserDetailsService { + private static final String ACCOUNT_TYPE = SecurityConstants.DEF_ACCOUNT_TYPE; + + @Resource + private UserService userService; + + @Override + public boolean supports(String accountType) { + return ACCOUNT_TYPE.equals(accountType); + } + + @Override + public UserDetails loadUserByUsername(String username) { + LoginAppUser loginAppUser = userService.findByUsername(username); + if (loginAppUser == null) { + throw new InternalAuthenticationServiceException("用户名或密码错误"); + } + return checkUser(loginAppUser); + } + + @Override + public SocialUserDetails loadUserByUserId(String openId) { + LoginAppUser loginAppUser = userService.findByOpenId(openId); + return checkUser(loginAppUser); + } + + @Override + public UserDetails loadUserByMobile(String mobile) { + LoginAppUser loginAppUser = userService.findByMobile(mobile); + return checkUser(loginAppUser); + } + + private LoginAppUser checkUser(LoginAppUser loginAppUser) { + if (loginAppUser != null && !loginAppUser.isEnabled()) { + throw new DisabledException("用户已作废"); + } + return loginAppUser; + } +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailsByNameServiceFactoryWrapper.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailsByNameServiceFactoryWrapper.java new file mode 100644 index 00000000..39f02f2b --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailsByNameServiceFactoryWrapper.java @@ -0,0 +1,59 @@ +package com.central.oauth.service.impl; + +import com.central.oauth.service.ZltUserDetailsService; +import lombok.Setter; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.*; +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; +import org.springframework.util.Assert; + +/** + * 重写 UserDetailsByNameServiceWrapper 支持多帐户类型 + * + * @author zlt + * @version 1.0 + * @date 2021/7/24 + * @see UserDetailsByNameServiceWrapper + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +public class UserDetailsByNameServiceFactoryWrapper implements + AuthenticationUserDetailsService, InitializingBean { + @Setter + private UserDetailServiceFactory userDetailServiceFactory; + + public UserDetailsByNameServiceFactoryWrapper() { + + } + + public UserDetailsByNameServiceFactoryWrapper(final UserDetailServiceFactory userDetailServiceFactory) { + Assert.notNull(userDetailServiceFactory, "userDetailServiceFactory cannot be null."); + this.userDetailServiceFactory = userDetailServiceFactory; + } + + /** + * Check whether all required properties have been set. + * + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + @Override + public void afterPropertiesSet() { + Assert.notNull(this.userDetailServiceFactory, "UserDetailServiceFactory must be set"); + } + + /** + * Get the UserDetails object from the wrapped UserDetailsService implementation + */ + @Override + public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException { + ZltUserDetailsService userDetailsService; + if (authentication instanceof PreAuthenticatedAuthenticationToken) { + userDetailsService = this.userDetailServiceFactory.getService((Authentication)authentication.getPrincipal()); + } else { + userDetailsService = this.userDetailServiceFactory.getService(authentication); + } + return userDetailsService.loadUserByUsername(authentication.getName()); + } +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/tenant/OauthAuthorizeAspect.java b/zlt-uaa/src/main/java/com/central/oauth/tenant/OauthAuthorizeAspect.java index 3bc7bd93..8dec5042 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/tenant/OauthAuthorizeAspect.java +++ b/zlt-uaa/src/main/java/com/central/oauth/tenant/OauthAuthorizeAspect.java @@ -1,14 +1,13 @@ package com.central.oauth.tenant; import com.central.common.context.TenantContextHolder; -import com.central.common.feign.UserService; import com.central.common.model.LoginAppUser; +import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth2.common.token.TenantUsernamePasswordAuthenticationToken; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.oauth2.common.util.OAuth2Utils; import org.springframework.stereotype.Component; @@ -29,8 +28,11 @@ import java.util.Map; @Component @Aspect public class OauthAuthorizeAspect { - @Autowired - private UserService userService; + private final UserDetailServiceFactory userDetailsServiceFactory; + + public OauthAuthorizeAspect(UserDetailServiceFactory userDetailsServiceFactory) { + this.userDetailsServiceFactory = userDetailsServiceFactory; + } @Around("execution(* org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(..))") public Object doAroundMethod(ProceedingJoinPoint joinPoint) throws Throwable { @@ -43,11 +45,14 @@ public class OauthAuthorizeAspect { String requestClientId = parameters.get(OAuth2Utils.CLIENT_ID); //判断是否不同租户单点登录 if (!requestClientId.equals(clientId)) { + Object details = tenantToken.getDetails(); try { TenantContextHolder.setTenant(requestClientId); //重新查询对应该租户的角色等信息 - LoginAppUser user = userService.findByUsername(tenantToken.getName()); + LoginAppUser user = (LoginAppUser)userDetailsServiceFactory.getService(tenantToken) + .loadUserByUsername(tenantToken.getName()); tenantToken = new TenantUsernamePasswordAuthenticationToken(user, tenantToken.getCredentials(), user.getAuthorities(), requestClientId); + tenantToken.setDetails(details); args[3] = tenantToken; } finally { TenantContextHolder.clear(); diff --git a/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationProvider.java b/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationProvider.java index 8dc7e4bd..c76d0ddc 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationProvider.java +++ b/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationProvider.java @@ -1,7 +1,7 @@ package com.central.oauth.tenant; +import com.central.oauth.password.PasswordAuthenticationProvider; import com.central.oauth2.common.token.TenantUsernamePasswordAuthenticationToken; -import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; @@ -14,7 +14,7 @@ import org.springframework.security.core.userdetails.UserDetails; * Blog: https://zlt2000.gitee.io * Github: https://github.com/zlt2000 */ -public class TenantAuthenticationProvider extends DaoAuthenticationProvider { +public class TenantAuthenticationProvider extends PasswordAuthenticationProvider { @Override protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) { diff --git a/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationSecurityConfig.java b/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationSecurityConfig.java index 88b75d71..1d00c9a9 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationSecurityConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/tenant/TenantAuthenticationSecurityConfig.java @@ -1,6 +1,6 @@ package com.central.oauth.tenant; -import com.central.oauth.service.ZltUserDetailsService; +import com.central.oauth.service.impl.UserDetailServiceFactory; import org.springframework.security.config.annotation.SecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.crypto.password.PasswordEncoder; @@ -16,19 +16,19 @@ import org.springframework.stereotype.Component; */ @Component public class TenantAuthenticationSecurityConfig extends SecurityConfigurerAdapter { - private ZltUserDetailsService userDetailsService; + private final UserDetailServiceFactory userDetailsServiceFactory; - private PasswordEncoder passwordEncoder; + private final PasswordEncoder passwordEncoder; - public TenantAuthenticationSecurityConfig(ZltUserDetailsService userDetailsService, PasswordEncoder passwordEncoder) { - this.userDetailsService = userDetailsService; + public TenantAuthenticationSecurityConfig(UserDetailServiceFactory userDetailsServiceFactory, PasswordEncoder passwordEncoder) { + this.userDetailsServiceFactory = userDetailsServiceFactory; this.passwordEncoder = passwordEncoder; } @Override public void configure(HttpSecurity http) { TenantAuthenticationProvider provider = new TenantAuthenticationProvider(); - provider.setUserDetailsService(userDetailsService); + provider.setUserDetailsServiceFactory(userDetailsServiceFactory); provider.setPasswordEncoder(passwordEncoder); http.authenticationProvider(provider); } diff --git a/zlt-web/back-web/src/main/resources/static/index.html b/zlt-web/back-web/src/main/resources/static/index.html index 3c5454b2..b8ba735d 100644 --- a/zlt-web/back-web/src/main/resources/static/index.html +++ b/zlt-web/back-web/src/main/resources/static/index.html @@ -4,7 +4,7 @@ - Demo + zlt-mp diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html b/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html index e0352e1b..1bbbe9c5 100644 --- a/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html +++ b/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html @@ -46,11 +46,12 @@ { sort: true, templet: function (d) { return util.toDateString(d.expiration); - }, title: '到期时间' + }, width:180, title: '到期时间' }, {field: 'username',width:180, sort: true, title: '用户名'}, {field: 'grantType',width:180, sort: true, title: '授权类型'}, {field: 'clientId',width:180, sort: true, title: '所属应用'}, + {field: 'accountType',width:180, sort: true, title: '账号类型'}, {align: 'center',width:100, toolbar: '#tokens-table-bar', title: '操作'} ]] });