SpringMVC整合SpringSession 实现sessiong

时间:2022-11-19 09:36:29

一、在pom.xml添加springsession

?
1
2
3
4
5
6
<!--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版本以上

?
1
2
3
4
5
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-context</artifactid>
  <version>4.3.10.release</version>
</dependency>

三、applicationcontext.xml文件中添加四个bean类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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

原文链接:https://blog.csdn.net/qq_38553333/article/details/80117300