When springsecurity occurs SerializationException problem

When springsecurity occurs SerializationException problem

01 Abnormal occurrence scenario

  • When I use springsecurity, there is a problem with the token access interface after successful login.
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 Reasons for the problem

  • The log says very clearly, Unable to read JSON: Unrecognized field
  • Originally I thought there was a problem with the format during storage.
  • But I checked the tool class and storage code and determined that there is no problem.
  • Later I found out that my entity class was not serialized

03 Solution

  • Add the annotation @JsonIgnoreProperties(ignoreUnknown = true) to the entity class
@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;
    }
}

Guess you like

Origin blog.csdn.net/2302_77182979/article/details/134767796