springSecurity+redis反序列化失败--problem deserializing ‘setterless‘ property (“authorities“)
當(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android应用开发报告,androi
- 下一篇: 超市网店营销与接口测试