shiro org.apache.shiro.session.mgt.SimpleSession对象 反序列化失败
问题描述
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized field "valid" (class org.apache.shiro.session.mgt.SimpleSession), not marked as ignorable (9 known properties: "attributeKeys", "timeout", "startTimestamp", "expired", "lastAccessTime", "host", "id", "stopTimestamp", "attributes"])
本质
redis在存储SimpleSession对象时,不知为什么多存储了一个valid字段,导致无法反序列化
解决方案
方案一
redis序列化时,对key采用字符串序列化,对value采用默认jdk序列化方式
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.net.UnknownHostException;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//常用<String,Object>
RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置默认序列化方式 可以是:Jackson2JsonRedisSerializer GenericJackson2JsonRedisSerializer
// redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
// 设置key以字符串序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
return redisTemplate;
}
}
最终效果