报错内容:
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `org.springframework.security.core.authority.SimpleGrantedAuthority` from Array value (token `JsonToken.START_ARRAY`)
redis里存的对应的json格式:
"authorities": [
"java.util.ArrayList",
[
[
"org.springframework.security.core.authority.SimpleGrantedAuthority",
{
"role": "admin",
"authority": "admin"
}
],
[
"org.springframework.security.core.authority.SimpleGrantedAuthority",
{
"role": "common",
"authority": "common"
}
]
]
],
原因: 字符串反序列化是,需要对应的对象有空参构造函数
而SimpleGrantedAuthority是只有有参构造,并且被final修饰不能继承重写
解决办法:
需要对该类进行手动进行反序列化
步骤:
第一步:自定义的反序列化器
public class SimpleGrantedAuthorityDeserializer extends StdDeserializer{ public SimpleGrantedAuthorityDeserializer() { super(SimpleGrantedAuthority.class); } @Override public SimpleGrantedAuthority deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode jsonNode = p.getCodec().readTree(p); Iterator elements = jsonNode.elements(); while (elements.hasNext()) { JsonNode next = elements.next(); JsonNode authority = next.get("authority"); if(ObjectUtils.isEmpty(authority)){ continue; } return new SimpleGrantedAuthority(authority.asText()); } return null; } }
第二步:将构造器加入到ObjectMapper 中
@Bean
@ConditionalOnMissingBean(RedisSerializer.class)
@SuppressWarnings("all")
public RedisSerializer
注意: ObjectMapper 不能new ,只能导入全局的,否则其他位置不生效
其他没有空参够够也会出现该问题,也需要注意



