使用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。

转载于:https://www.cnblogs.com/kintanx/p/10776614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 添加Redis依赖库 在项目的pom.xml文件中添加Redis的依赖库。 ```xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置Redis连接池 在SpringMVC的配置文件中添加Redis的连接池配置。 ```xml <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> <<property name="password" value="password"/>> <property name="poolConfig"> <bean class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1000"/> <property name="maxIdle" value="100"/> <property name="minIdle" value="10"/> <property name="maxWaitMillis" value="10000"/> </bean> </property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> ``` 3. 配置Session共享SpringMVC的配置文件中添加Session共享的配置。 ```xml <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800"/> </bean> <bean class="org.springframework.session.web.http.CookieHttpSessionIdResolver"> <constructor-arg name="cookieName" value="SESSION_ID"/> <property name="cookiePath" value="/"/> <property name="cookieMaxAge" value="-1"/> </bean> ``` 4. 启用Session共享SpringMVC的配置文件中启用Session共享。 ```xml <bean class="org.springframework.session.web.http.SessionRepositoryFilter"> <constructor-arg> <bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository"> <constructor-arg ref="redisTemplate"/> </bean> </constructor-arg> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="sessionAttributeStore"> <bean class="org.springframework.session.web.http.SessionRepositoryFilter"> <constructor-arg> <bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository"> <constructor-arg ref="redisTemplate"/> </bean> </constructor-arg> </bean> </property> </bean> ``` 5. 测试 启动项目后,访问任意一个页面,然后在Redis中就可以看到Session的信息了。 ```shell redis-cli 127.0.0.1:6379> keys * 1) "spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b" 127.0.0.1:6379> get spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b "\xac\xed\x00\x05sr\x00\x17java.util.HashMap\x85\x95\x1d\x9b\x94\xee\x0c\x00\x00\x03I\x00\x05valuexr\x00\x10java.lang.String\x86\xac\xed\x00\x05sr\x00\x11java.lang.String\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01Ljava/lang/String;L\x00\x04namet\x00\x12Ljava/lang/String;L\x00\x08usernameq\x00~\x00\x01sq\x00~\x00\x0bpasswordq\x00~\x00\x0b" ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值