【发布时间】:2021-02-15 22:42:29
【问题描述】:
我已经成功获得了使用令牌存储 JwtTokenStore(JwtAccessTokenStore) 的解码细节,但现在它需要使用 redis 以便我可以撤销令牌。
这里是我的代码:
@Bean
public TokenStore tokenStore() {
return new RedisTokenStore(redisConnectionFactory);
// return new JwtTokenStore(defaultAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter defaultAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setAccessTokenConverter(new CustomJWTAccessTokenConverter());
try {
converter.afterPropertiesSet();
} catch (Exception e) {
e.printStackTrace();
}
converter.setKeyPair(this.keyPair());
return converter;
}
还有我的 customjwtaccesstokenconverter :
public class CustomJWTAccessTokenConverter extends DefaultAccessTokenConverter {
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
OAuth2Authentication authentication
= super.extractAuthentication(claims);
authentication.setDetails(claims);
return authentication;
}
}
令牌增强器:
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
Map<String, Object> setAdditionalInformation = (Map<String, Object>) authentication.getUserAuthentication().getDetails();
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(setAdditionalInformation);
return accessToken;
}
我不知道,什么时候使用 redistokenstore。它没有转到 CustomJWTAccessTokenConverter,因为当我尝试获取其他信息(decodeDetails)时返回 null。
OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
OAuth2AuthenticationDetails authenticationDetails = (OAuth2AuthenticationDetails) authentication.getDetails();
Map<String, Object> decodeDetails = (Map<String, Object>) authenticationDetails.getDecodedDetails();
【问题讨论】:
标签: spring-security jwt spring-oauth2