回复 朱小悬:我是将jwt存在redis里的,登出的时候从redis删除就行了。
这是代码:
@Configuration
public class TokenStoreConfig {
@Configuration
@ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "jdbc")
@Import(AuthJdbcTokenStore.class)
public static class JdbcTokenConfig {
}
@Configuration
@ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "redis")
@Import(AuthRedisTokenStore.class)
public static class RedisTokenConfig {
}
@Configuration
@ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "jwt")
@Import(AuthJwtTokenStore.class)
public static class AuthJwtTokenConfig {
}
@Configuration
@ConditionalOnProperty(prefix = "auth.token.store", name = "type", havingValue = "redisJwt", matchIfMissing = true)
@Import(RedisJwtTokenStore.class)
public static class ResJwtTokenConfig {
}
@Bean
public TokenEnhancerChain tokenEnhancerChain() {
return new TokenEnhancerChain();
}
}
RedisJwtTokenStore配置如下:
public class RedisJwtTokenStore {
@Resource
private TokenEnhancerChain tokenEnhancerChain;
@Resource
private RedisConnectionFactory redisConnectionFactory;
@Bean
public TokenStore redisTokenStore() {
return new RedisTokenStore(redisConnectionFactory);
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("test");
return converter;
}
@PostConstruct
public void init(){
List<TokenEnhancer> enhancers = new ArrayList<>(2);
enhancers.add(jwtTokenEnhancer());
enhancers.add(jwtAccessTokenConverter());
tokenEnhancerChain.setTokenEnhancers(enhancers);
}
/**
* jwt 生成token 定制化处理
* 添加一些额外的用户信息到token里面
*
* @return TokenEnhancer
*/
@Bean
@ConditionalOnMissingBean(DefaultJwtTokenEnhancer.class)
public TokenEnhancer jwtTokenEnhancer() {
return new DefaultJwtTokenEnhancer();
}
}