我是用来存储oauth用户信息,通过缓存来做缓冲,结合springcache使用
一开始在构造函数上配置@JsonCreator,后事可以反序列化,不过不知某天什么改动有报此错误。
通过编写一个自定义的反序列化器解决此问题
class SimpleGrantedAuthorityDeserializer extends StdDeserializer<SimpleGrantedAuthority> {
public SimpleGrantedAuthorityDeserializer() {
super(SimpleGrantedAuthority.class);
}
@Override
public SimpleGrantedAuthority deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode tree = p.getCodec().readTree(p);
return new SimpleGrantedAuthority(tree.get("authority").textValue());
}
}
然后在redis的反序列化使用Jackson2JsonRedisSerializer反序列化,在里面配置ObjectMapper
配置中加上
objectMapper.registerModule(new SimpleModule().addDeserializer( SimpleGrantedAuthority.class, new SimpleGrantedAuthorityDeserializer()));
最后说下jackson+redis序列化会根据get方法自动序列没有此字段的序列化字段出来,SimpleGrantedAuthority中只有role字段 ,没有authority字段,有getAuthority方法赋值role值。
"org.springframework.security.core.authority.SimpleGrantedAuthority",
{
"role": "sys_log_del",
"authority": "sys_log_del"
}