redis 处理session共享

本文介绍如何将Spring Session与Redis进行集成,实现跨服务器的会话共享。具体步骤包括:在pom.xml中添加相关依赖;在web.xml中配置过滤器;在jdbc.properties中设置Redis连接信息;在spring-hibernate.xml中定义Redis连接池和会话配置;以及在security.xml中更新会话注册。通过这些步骤,可以有效地利用Redis作为会话存储,提高应用的可伸缩性和性能。

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

redis 安装
http://www.runoob.com/redis/redis-install.html


redis 参考
http://www.cnblogs.com/andyfengzp/p/6434287.html

http://dorole.com/1422/

http://blog.youkuaiyun.com/hdf734839030/article/details/52293275

1.pom.xml修改
增加
<!-- session redis依赖  start-->
<dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-data-redis</artifactId>
 <version>1.3.0.RELEASE</version>
</dependency>
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.9.0</version>
</dependency>
<!-- session redis依赖  end -->


2.web.xml修改
在xml的前部位置(其他listner之前,加载配置文件之后)增加
    <filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
<filter-mapping>
   <filter-name>springSessionRepositoryFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>




3.jdbc.properties修改
增加
redis.hostname=127.0.0.1
redis.port=6379
redis.pwd=123456


4.spring-hibernate.xml修改
增加
<!-- redis的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <property name="maxTotal" value="100" />
   <property name="maxIdle" value="10" />
</bean>

<bean id="jedisConnectionFactory"
     class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
   <property name="hostName" value="${redis.hostname}"/>
   <property name="port" value="${redis.port}"/>
   <property name="password" value="${redis.pwd}" />
   <property name="timeout" value="3000"/>
   <property name="usePool" value="true"/>
   <property name="poolConfig" ref="jedisPoolConfig"/>
</bean>

<!-- 为不同项目设置不同的cookie键值,避免产生host:端口部署不同项目产生覆盖cookie的问题 -->
<bean id="cookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer" >
<property name="cookieName" value="修改成自己的项目名-cookie"></property>
</bean>
<bean id="redisHttpSessionConfiguration"
      class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
    <property name="maxInactiveIntervalInSeconds" value="1800"/>
    <property name="redisNamespace" value="修改成自己的项目名"></property>
    <property name="cookieSerializer" ref="cookieSerializer"></property>
</bean>


5.security.xml修改(如果不存在,这一步可以省略)
<bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />


修改为
<bean id="sessionRegistry" class="org.springframework.session.security.SpringSessionBackedSessionRegistry">
<constructor-arg name="sessionRepository" ref="sessionRepository"></constructor-arg>
</bean>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值