您的位置:首页 > 编程语言 > Java开发

SpringMVC整合SpringSession 实现session共享

2018-04-28 10:37 405 查看

一、在pom.xml添加springSession

        <!--springSession-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

二、确保spring是4.3.10.RELEASE版本以上

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
三、applicationContext.xml文件中添加四个bean类
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="180"></property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"></property>
<property name="port" value="6379"></property>
<property name="poolConfig" ref="jedisPoolConfig"></property>
</bean>
<bean id="defaultCookieSerializer"
class="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="cookieName" value="springSession"></property>
<property name="cookiePath" value="/"></property>
<property name="cookieMaxAge" value="3600"/>
<property name="useHttpOnlyCookie" value="true"/>
<property name="domainName" value=".qs.com"/>
</bean>
这样就可以实现session共享,Nginx下的tomcat集群也是这样的

原理就是:通过SpringSession对servlet带的session进行封装,接管session

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: