欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC整合SpringSession 实现sessiong

程序员文章站 2023-12-01 10:20:46
一、在pom.xml添加springsession ...

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