多个springmvc项目共用同一个redis导致session冲突的问题 的解决方案

3 篇文章 0 订阅
1 篇文章 0 订阅

 由于redis来做session的统一管理插件,如果多个项目使用同一个redis来管理session的话,那么session很有可能会出现冲突。

下图:默认session在redis中的key值是spring:session:sessions:sessionId,如果多个项目中出现了相同的sessionId的情况下就会在redis中出现key值被覆盖

那么解决方案就很简单:

方案一、springmvc配置方案

applicationContext.xml文件中增加配置

<property name="redisNamespace" value="business"></property>
  <!-- spring redis session配置-->
    <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!-- 由于business、sales共用同一个redis,所以此处需要修改session的key,使其不和其他工程冲突  -->
        <property name="redisNamespace" value="business"></property>
        <property name="defaultRedisSerializer" ref="redisJdkSerializer" />
        <!-- sesson 过期时间-->
        <property name="maxInactiveIntervalInSeconds" value="1800"/> 
        <property name="cookieSerializer" ref="defaultCookieSerializer"/>
    </bean>

方案二、springboot配置方案

配置文件配置方式:yml或properties

spring.session.redis.namespace=xxx.xxx.xxx.test

 启动类配置

@EnableRedisHttpSession(redisNamespace = “xxx.xxx.xxx.test”,maxInactiveIntervalInSeconds=600)

选择配置方式就不要再用注解方式,@EnableRedisHttpSession优先级高于配置文件的方式,会导致配置不生效。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值