Oauth2自定义check_token返回解析参数(自定义UserAuthenticationConverter)
首先来看Oauth 解析token流程
而DefaultAccessTokenConverter使用DefaultUserAuthenticationConverter来解析token信息
可以看到DefaultAccessTokenConverter可以设置 UserAuthenticationConverter,因为我们可以自定义AccessTokenConverter替换也可以自定义UserAuthenticationConverter替换,这次我就展示替换默认的UserAuthenticationConverter最简单 的方法实现
1.继承默认的转换器重写转换用户证明信息方法,也可以直接实现UserAuthenticationConverter接口
public class XQUserAuthenticationConverter extends DefaultUserAuthenticationConverter {
@Override
public Map<String, ?> convertUserAuthentication(Authentication authentication) {
Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put(USERNAME, authentication.getName());
Object principal = authentication.getPrincipal();
if(principal instanceof SysUser){
SysUser sysUser = new SysUser();
sysUser = (SysUser) principal;
if(StringUtils.isNotEmpty(sysUser.getOpenId())){
response.put("userId",sysUser.getOpenId());
response.remove(USERNAME);
}
}
if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
}
return response;
}
}
/**
* OAuth2 授权服务器配置
*
* @author zlt
* @date 2018/10/24
*/
@Configuration
@EnableAuthorizationServer
@AutoConfigureAfter(AuthorizationServerEndpointsConfigurer.class)
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
/**
* 注入authenticationManager 来支持 password grant type
*/
@Autowired
private AuthenticationManager authenticationManager;
@Resource
private UserDetailsService userDetailsService;
@Autowired
private TokenStore tokenStore;
@Autowired(required = false)
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Autowired(required = false)
private TokenEnhancer tokenEnhancer;
@Autowired
private WebResponseExceptionTranslator webResponseExceptionTranslator;
@Autowired
private RedisClientDetailsService clientDetailsService;
@Autowired
private RandomValueAuthorizationCodeServices authorizationCodeServices;
/**
* 配置身份认证器,配置认证方式,TokenStore,TokenGranter,OAuth2RequestFactory
* @param endpoints
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
if (jwtAccessTokenConverter != null) {
if (tokenEnhancer != null) {
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(
Arrays.asList(tokenEnhancer, jwtAccessTokenConverter));
endpoints.tokenEnhancer(tokenEnhancerChain);
} else {
endpoints.accessTokenConverter(jwtAccessTokenConverter);
}
}
DefaultAccessTokenConverter defaultAccessTokenConverter=new DefaultAccessTokenConverter();
defaultAccessTokenConverter.setUserTokenConverter(new XQUserAuthenticationConverter());
endpoints.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
.authorizationCodeServices(authorizationCodeServices)
.exceptionTranslator(webResponseExceptionTranslator).accessTokenConverter(defaultAccessTokenConverter);
}
/**
* 配置应用名称 应用id
* 配置OAuth2的客户端相关信息
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService);
clientDetailsService.loadAllClientToCache();
}
/**
* 对应于配置AuthorizationServer安全认证的相关信息,创建ClientCredentialsTokenEndpointFilter核心过滤器
* @param security
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
security
.tokenKeyAccess("isAuthenticated()")
.checkTokenAccess("permitAll()")
//让/oauth/token支持client_id以及client_secret作登录认证
.allowFormAuthenticationForClients();
}
@Bean
public TokenEnhancer accessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
//配置自定义转换器
DefaultAccessTokenConverter tokenConverter = new DefaultAccessTokenConverter();
tokenConverter.setUserTokenConverter(new XQUserAuthenticationConverter());
converter.setAccessTokenConverter(tokenConverter);
return converter;
}
// endpoints
}