使用spring-session共享springmvc项目的session

一、说在前面

  1. spring mvc项目,使用nginx,tomcat部署。
  2. 之前没做session共享,而nginx采用sticky模块进行分发。
    但发现有时不能正确地指向同一台服务器,从而导致session丢失。
    一直不能很好地定位原因,于是有了做session共享的计划。

二、操作及配置

  • pom添加依赖
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>1.3.0.RELEASE</version>
</dependency>
  • spring-session.xml配置
<context:annotation-config/>
<bean id="redisHttpSessionConfiguration"  
        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
    <property name="maxInactiveIntervalInSeconds" value="3600" />
    <property name="redisNamespace" value="web:xxxx:prod"/>
</bean>
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="127.0.0.1" />
    <property name="port" value="6379" />
    <property name="database" value="5" />
    <property name="password" value="my_password" />
</bean>
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
  • web.xml配置
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-session.xml</param-value>
</context-param>
<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

三、注意及说明

  1. 不加上
    static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"
    的话,有可能会因为redis权限不够导致以下报错:”ERR unknown command 'CONFIG'“
  2. pom dependency各版本地址: https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis
  3. 1.2版本起步要求spring4,2.x要求spring5
    由于项目本身spring版本的差异,spring-session-data-redis版本的不同可能会有方法参数不存在,无对应bean定义之类错误。请参考2中的地址尽量选择匹配的版本。
  4. 2.x以下版本,RedisHttpSessionConfiguration中,即使配置了redisNamespace参数,redisKey还是会有'spring:session:'前缀,无法去掉。而2.x版本,修复了此问题。
  5. web.xml中filter一定要叫'springSessionRepositoryFilter'这个名字,否则会报错。
    此filter配置要尽量放在前面,尤其是它之前的filter不能调用request.getSession()方法。
  6. 由于使用了maxInactiveIntervalInSeconds配置,由redis负责接管Session,原来web.xml里配置的Session超时时间就会失效了。
  7. 保存session对象默认使用JDK序列化方式,这就要求保存在session的对象都要实现Serializable接口。
  8. 保存session支持自定义方式,例如json。

猜你喜欢

转载自www.cnblogs.com/kintanx/p/10776614.html
今日推荐