spring-security-oauth2-authorization-serverRedis存储token实现
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.server.authorization.RedisOAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenSerializer;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenSerializerFactory;
@Configuration
public class RedisTokenConfig {
@Bean
public RedisOAuth2AuthorizationService redisOAuth2AuthorizationService(RedisConnectionFactory redisConnectionFactory) {
return new RedisOAuth2AuthorizationService(redisConnectionFactory);
}
@Bean
public OAuth2TokenSerializer<OAuth2TokenCustomizer> oAuth2TokenSerializer(RedisConnectionFactory redisConnectionFactory) {
OAuth2TokenSerializerFactory factory = new OAuth2TokenSerializerFactory();
factory.setRedisConnectionFactory(redisConnectionFactory);
return factory.createSerializer();
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
这个配置类定义了Redis存储的相关Bean,包括RedisOAuth2AuthorizationService
用于存储授权信息,OAuth2TokenSerializer
用于序列化和反序列化令牌,以及基本的RedisTemplate
用于与Redis交互。这些Bean可以被注入到OAuth2AuthorizationServerConfiguration
中,以确保授权服务器使用Redis作为令牌存储。
评论已关闭