背景
整合SpringSecurity过程中 ,对User类进行了更改,导致Redis在反序列化时出现了问题,Google或者StackOverflow网速较慢,所以就把解决方案放在这,作为记录。
package com.how2j.copy.pojo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Table(name = "`user`")
@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements UserDetails {
public User() {
}
@Id
@Column(name = "`Id`")
@GeneratedValue(generator = "JDBC")
private Integer id;
//authorities
@JsonIgnore
public List<Authority> authorities = new ArrayList<>();
public void setAuthorities(List<Authority> authorities) {
this.authorities = authorities;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> list = new ArrayList<>();
for(Authority authority : authorities){
list.add(new SimpleGrantedAuthority(authority.getAuthority()));
}
return list;
}
这是我的 user 类 ,把一些不重要的部分删掉了 ,
报错类似于这个
Caused by: org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.InvalidClassException: weixin.popular.bean.token.Token; local class incompatible: stream classdesc serialVersionUID = -584152862903853930, local class serialVersionUID = 8841433872811285796
stackoverflow上很多人只说了
添加在属性上 @JsonIgnore 注解 ,其实是缺了一小部分,像我这类新手依然会碰上错误。
那就是在 类上加入@JsonIgnoreProperties(ignoreUnknown = true) 注解
否则就会报Unrecognized field "identityFederationEnabled" importing old realm json
这个错误
总之 需要改的地方有两处,
-
1.
-
1.
@JsonIgnore
public List<Authority> authorities = new ArrayList<>();
-
2.
@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements UserDetails {