一、在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

01-30 03:03