日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springSecurity+redis反序列化失败--problem deserializing ‘setterless‘ property (“authorities“)

發(fā)布時(shí)間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springSecurity+redis反序列化失败--problem deserializing ‘setterless‘ property (“authorities“) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

當(dāng)時(shí)用springSecurity+redis存儲(chǔ)/讀取用戶信息的時(shí)候,發(fā)生報(bào)錯(cuò)。

報(bào)錯(cuò)異常

Class:class org.springframework.data.redis.serializer.SerializationException LocalizedMessage:Could not read JSON: Problem deserializing 'setterless' property ("authorities"): no way to handle typed deser with setterless yet at [Source: (byte[])"{"@class":"com.jia.pojo.User" ,"id":1,"username":"root", "paassword":"{bcrypt}$2a$10$zI7PbpjGfcsxblk50lTlr.kLW65lAAoZU6Ic.a8b1.I6V4XXk/Je.", "phone":"15061964947","enabled":true,"accountNonExpired":true,"accountNonLocked":true, "credentialsNonExpired":true,"roles":["java.util.ArrayList",[]],"role":null, "authorities":["java.util.HashSet",[]]}"; line: 1, column: 314] (through reference chain: com.jia.pojo.User["authorities"]); nested exception is com.fasterxml.jackson.databind.exc. InvalidDefinitionException: Problem deserializing 'setterless' property ("authorities"): no way to handle typed deser with setterless yet at [Source: (byte[])" {"@class":"com.jia.pojo.User","id":1,"username":"root","password":"{bcrypt}$2a$10$zI7PbpjGfcsxblk 50lTlr.kLW65lAAoZU6Ic.a8b1.I6V4XXk/Je.", "phone":"15061964947","enabled":true,"accountNonExpired":true,"accountNonLocked":true, "credentialsNonExpired":true,"roles":["java.util.ArrayList",[]],"role":null, "authorities":["java.util.HashSet",[]]}"; line: 1, column: 314] (through reference chain: com.jia.pojo.User["authorities"])

錯(cuò)誤原因

這一段異常錯(cuò)誤最主要的點(diǎn)在于這:

Problem deserializing 'setterless' property ("authorities"): no way to handle typed deser with setterless yet at

大概意思就是反序列化屬性(“authorities”)的問題:沒有setterless 方法處理輸入值。

說白了就是在我們?cè)O(shè)置redis時(shí)redisConfig我們采用的是jackson,而Jackson采用的反序列化時(shí)會(huì)調(diào)用屬性的set方法注入值。

RedisConfig中自定義RedisTemplate代碼:

public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(factory);//序列化配置Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);//om.activateDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 過期方法,采用下面代替om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(om);//String序列化StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}

這里是配置寫入讀取redis值時(shí)采用jackson進(jìn)行操作。

// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);

但我們自定義的user類缺只有重寫的getAuthorities()方法。沒有g(shù)etAuthorities()方法,因此jackson反序列化的時(shí)候就會(huì)失敗。

好了,知道了原因,解決方法就簡單了。

解決方法

1、手動(dòng)添加authorities屬性。添加getAuthorities()方法

@TableField(exist = false) private Collection<? extends GrantedAuthority> authorities; public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {this.authorities = authorities;}

2、在getAuthorities()上添加@JsonIgnore,序列化時(shí)忽略此選項(xiàng)。

@Override@JsonIgnorepublic Collection<? extends GrantedAuthority> getAuthorities() {Set<SimpleGrantedAuthority> authorities=new HashSet<>();roles.forEach(role -> {SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());authorities.add(authority);});return authorities;}

注意:如果你的前端需要authorities數(shù)據(jù)這個(gè)方法就不可以行。所以我還是建議第一種方法。可以在保證數(shù)據(jù)完整行的前提下完美解決問題。

搞定

要是解決了你的問題記得幫我點(diǎn)個(gè)贊哦!

總結(jié)

以上是生活随笔為你收集整理的springSecurity+redis反序列化失败--problem deserializing ‘setterless‘ property (“authorities“)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。