SpringMVC整合SpringSession 实现session共享

本文介绍如何使用 Spring Session 在多个应用间实现 Session 的共享。主要步骤包括:在 pom.xml 中添加 spring-session 依赖;确保使用的是 Spring 4.3.10.RELEASE 版本或以上;配置 applicationContext.xml 文件中的四个 bean 类,包括 RedisHttpSessionConfiguration、JedisPoolConfig、JedisConnectionFactory 和 DefaultCookieSerializer。这些配置使得 Servlet 中的 Session 能够被 SpringSession 封装并实现共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值