Springboot Redis 如何缓存 OAuth2Authorization ?
版本
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
<version>3.1.0</version>
</dependency>
RedisTemplate配置
@Bean(name = "redisTemplate") @ConditionalOnClass(RedisOperations.class)
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
RedisKeyStringSerializer keyStringSerializer = new RedisKeyStringSerializer(keyPrefix);
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// Key 采用String 的序列化方式
template.setKeySerializer(keyStringSerializer);
// hash 的 key 也采用 String 的序列化方式
template.setHashKeySerializer(keyStringSerializer);
// value 序列化方式采用 jackson
template.setValueSerializer(RedisSerializer.json());
// hash的value序列化方式采用jackson
template.setHashValueSerializer(RedisSerializer.json());
template.afterPropertiesSet();
return template;
}
OAuth2Authorization把这个对象缓存进了redis, 序列化不出来,它里面的AuthorizationGrantType没提供无参构造.
如果将Redis配置成RedisSerializer.java()
就会导致不好查看,不知道还没有其他的解决办法。
回答:
public abstract class AuthorizationGrantTypeMixin { @JsonCreator
public AuthorizationGrantTypeMixin(@JsonProperty("value") String value) { }
}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(AuthorizationGrantType.class, AuthorizationGrantTypeMixin.class);
RedisSerializer<Object> serializer = new GenericJackson2JsonRedisSerializer(objectMapper);
template.setDefaultSerializer(serializer);
以上是 Springboot Redis 如何缓存 OAuth2Authorization ? 的全部内容, 来源链接: utcz.com/p/945245.html