当springsecurity出现SerializationException问题
01 异常发生场景
当我使用springsecurity时,登录成功后携带token访问接口出了问题
org. springframework. data. redis. serializer. SerializationException: Could not read JSON : Unrecognized field "username" ( class com. example. demo. entity. LoginUser) , not marked as ignorable ( 2 known properties: "msUser" , "authorities" ] )
02 问题发生的原因
日志说的很清楚,无法读取 JSON:无法识别的字段 原本我以为是存储时格式有问题 但是检查了工具类和存储时的代码,确定了没问题 后来发现我的实体类没有序列化
03 解决方式
在实体类上加上注解@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties ( ignoreUnknown = true )
public class LoginUser implements UserDetails { private MsUser msUser; @Override public Collection < ? extends GrantedAuthority > getAuthorities ( ) { return null ; } @Override public String getPassword ( ) { return msUser. getPassword ( ) ; } @Override public String getUsername ( ) { return msUser. getUsername ( ) ; } @Override public boolean isAccountNonExpired ( ) { return true ; } @Override public boolean isAccountNonLocked ( ) { return true ; } @Override public boolean isCredentialsNonExpired ( ) { return true ; } @Override public boolean isEnabled ( ) { return true ; }
}