Cannot construct instance of `org.springframework.security.core.authority.SimpleGrantedAuthority` - Go语言中文社区

Cannot construct instance of `org.springframework.security.core.authority.SimpleGrantedAuthority`


我是用来存储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"

}

 

 

转载于:https://my.oschina.net/u/1579617/blog/3021547

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_34402408/article/details/92134715
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 14:18:44
  • 阅读 ( 724 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢