Springsession redis修改默认的序列化方法是什么
一、配置Spring-session-redis 的关键配置如下:
<!-- 设置Cookie domain 和 名称 --> <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"> <property name="domainName" value="${sso.cookie.domain}"/> <property name="cookieName" value="${sso.cookie.name}"/> <property name="cookieMaxAge" value="${sso.cookie.maxage}" /> <property name="cookiePath" value="${sso.cookie.path}" /> <!--<property name="domainNamePattern" value="^.+?\\.(\\w+\\.[a-z]+)$"/>--> </bean> <!-- 配置spring session --> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <!-- session超时设置 --> <property name="maxInactiveIntervalInSeconds" value="${spring.session.expiration}" /> <property name="cookieSerializer" ref="defaultCookieSerializer" /> </bean>登录后复制
二、查看 RedisHttpSessionConfiguration 源代码如下:
@Bean public RedisTemplate<Object, Object> sessionRedisTemplate( RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); if (this.defaultRedisSerializer != null) { template.setDefaultSerializer(this.defaultRedisSerializer); } template.setConnectionFactory(connectionFactory); return template; } @Bean public RedisOperationsSessionRepository sessionRepository( @Qualifier("sessionRedisTemplate") RedisOperations<Object, Object> sessionRedisTemplate, ApplicationEventPublisher applicationEventPublisher) { RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository( sessionRedisTemplate); sessionRepository.setApplicationEventPublisher(applicationEventPublisher); sessionRepository .setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); if (this.defaultRedisSerializer != null) { sessionRepository.setDefaultSerializer(this.defaultRedisSerializer); } String redisNamespace = getRedisNamespace(); if (StringUtils.hasText(redisNamespace)) { sessionRepository.setRedisKeyNamespace(redisNamespace); } sessionRepository.setRedisFlushMode(this.redisFlushMode); return sessionRepository; }登录后复制
创建RedisTemplate时,指定的默认序列化是:StringRedisSerializer,当 defaultRedisSerializer != null 时,重新指定了序列化方法。以下是 defaultRedisSerializer 的初始化代码:
@Autowired(required = false) @Qualifier("springSessionDefaultRedisSerializer") public void setDefaultRedisSerializer( RedisSerializer<Object> defaultRedisSerializer) { this.defaultRedisSerializer = defaultRedisSerializer; }登录后复制
因此,我们只需要创建名为 springSessionDefaultRedisSerializer 的Bean,及可修改默认序列化方法。
<bean id="springSessionDefaultRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />登录后复制
注:使用JSON序列化,在特定的场景,会报错,谨慎使用。
三、RedisTemplate 初始化代码如下:
public void afterPropertiesSet() { super.afterPropertiesSet(); boolean defaultUsed = false; if (defaultSerializer == null) { defaultSerializer = new JdkSerializationRedisSerializer( classLoader != null ? classLoader : this.getClass().getClassLoader()); } if (enableDefaultSerializer) { if (keySerializer == null) { keySerializer = defaultSerializer; defaultUsed = true; } if (valueSerializer == null) { valueSerializer = defaultSerializer; defaultUsed = true; } if (hashKeySerializer == null) { hashKeySerializer = defaultSerializer; defaultUsed = true; } if (hashValueSerializer == null) { hashValueSerializer = defaultSerializer; defaultUsed = true; } } if (enableDefaultSerializer && defaultUsed) { Assert.notNull(defaultSerializer, "default serializer null and not all serializers initialized"); } if (scriptExecutor == null) { this.scriptExecutor = new DefaultScriptExecutor<K>(this); } initialized = true; }登录后复制
从句式和语序上调整,可将原话重写为:"如果未指定默认值,使用JdkSerializationRedisSerializer。"。
特别提醒:如果已有session中存在旧有的序列化值,更换序列化方法后,要删除旧值,使用新的序列化进行调试,否则会报序列化错误。
特别提醒:如果已有session中存在旧有的序列化值,更换序列化方法后,要删除旧值,使用新的序列化进行调试,否则会报序列化错误。