Redis reports an error when integrating springsecurity: could not read JSON: unrecognized field “enabled”“

When springboot integrates springsecurity, an error occurs:

  By viewing the error reporting information, it is found that the errors are reported where redis is used. In the project, redis is used as a cache to store user information. Everything is normal when spring security is not integrated, but there is a problem after the integration. I checked several blogs on the Internet and found that there is a problem when redis serializes and stores user objects.

Key error messages:

Could not read JSON: Unrecognized field "enabled" (class com.xyc.community.entity.User), not marked as ignorable (11 known properties: "authorities", "status", "activationCode", "username", "createTime", "type", "id", "email", "headerUrl", "salt", "password"])
 at [Source: (byte[])"{"@class":"com.xyc.community.entity.User","id":134,"username":"www","password":"3d3aeebb9cd302ae581dfa8fedd86ceb","salt":"dfc00","email":"[email protected]","type":0,"status":1,"activationCode":null,"headerUrl":"http://images.nowcoder.com/head/134t.png","createTime":["java.util.Date",1555668837000],"enabled":true,"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true}"; line: 1, column: 318] (through reference chain: com.xyc.community.entity.User["enabled"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "enabled" (class com.xyc.community.entity.User), not marked as ignorable (11 known properties: "authorities", "status", "activationCode", "username", "createTime", "type", "id", "email", "headerUrl", "salt", "password"])
 at [Source: (byte[])"{"@class":"com.xyc.community.entity.User","id":134,"username":"www","password":"3d3aeebb9cd302ae581dfa8fedd86ceb","salt":"dfc00","email":"[email protected]","type":0,"status":1,"activationCode":null,"headerUrl":"http://images.nowcoder.com/head/134t.png","createTime":["java.util.Date",1555668837000],"enabled":true,"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true}"; line: 1, column: 318] (through reference chain: com.xyc.community.entity.User["enabled"])

You can see that several fields are added during serialization because the userdetails interface is implemented in the user class in order to integrate springsecurity. These methods have been rewritten

  terms of settlement:

Using the @ jsonignoreproperties annotation, you can ignore these fields when the user object is serialized

Add before user class:

@JsonIgnoreProperties({"enabled","accountNonExpired", "accountNonLocked", "credentialsNonExpired", "authorities"})

Read More: