feat(PreAuthenticatedUserDetailsService.java):刷新token再次认证的UserDetailsService根据客户端ID区分用户体系,根据认证方式区分方法调用。

This commit is contained in:
有来技术 2021-10-09 00:50:12 +08:00
parent 2d9a00c332
commit ec2cedacbc
2 changed files with 14 additions and 2 deletions

View File

@ -120,11 +120,15 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
tokenServices.setClientDetailsService(clientDetailsService);
tokenServices.setTokenEnhancer(tokenEnhancerChain);
PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
// 多用户体系下刷新token再次认证客户端ID和 UserDetailService 的映射Map
Map<String, UserDetailsService> clientUserDetailsServiceMap = new HashMap<>();
clientUserDetailsServiceMap.put(AuthConstants.ADMIN_CLIENT_ID, sysUserDetailsService);
clientUserDetailsServiceMap.put(AuthConstants.APP_CLIENT_ID, memberUserDetailsService);
clientUserDetailsServiceMap.put(AuthConstants.WEAPP_CLIENT_ID, memberUserDetailsService);
// 重写刷新token再次认证的 AuthenticationManager中 UserDetailService根据客户端ID和认证方式获取用户认证信息 UserDetails
PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
provider.setPreAuthenticatedUserDetailsService(new PreAuthenticatedUserDetailsService<>(clientUserDetailsServiceMap));
tokenServices.setAuthenticationManager(new ProviderManager(Arrays.asList(provider)));
return tokenServices;

View File

@ -16,7 +16,7 @@ import org.springframework.util.Assert;
import java.util.Map;
/**
* UserDetailsService 工厂类
* 刷新token再次认证 UserDetailsService
*
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
* @date 2021/10/2
@ -49,6 +49,14 @@ public class PreAuthenticatedUserDetailsService<T extends Authentication> implem
default:
return memberUserDetailsService.loadUserByUsername(authentication.getName());
}
} else if (clientId.equals(AuthConstants.WEAPP_CLIENT_ID)) {
MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService;
switch (authenticationMethodEnum) {
case MOBILE:
return memberUserDetailsService.loadUserByMobile(authentication.getName());
default:
return memberUserDetailsService.loadUserByUsername(authentication.getName());
}
} else if (clientId.equals(AuthConstants.ADMIN_CLIENT_ID)) {
switch (authenticationMethodEnum) {
default: