spring-session实现session共享

HttpSession是通过Servlet容器创建和管理的,像Tomcat/Jetty都是保存在内存中的。但是我们把应用搭建成分布式的集群,然后利用LVS或Nginx做负载均衡,那么来自同一用户的Http请求将有可能被分发到多个不同的应用中,这样问题来了,如何保证不同的应用能够共享同一份session数据呢?最简单的想法,就是把session数据保存到内存以外的一个统一的地方,例如Memcached/Redis等数据库中。那问题又来了,如何替换掉Servlet容器创建和管理的HttpSession的实现呢?


1)利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略。这方面其实早就有开源项目了,例如memcached-session-manager(可以参考负载均衡+session共享(memcached-session-manager实现),以及tomcat-redis-session-manager。不过这种方式有个缺点,就是需要耦合Tomcat/Jetty等Servlet容器的代码。


2)设计一个Filter,利用HttpServletRequestWrapper,实现自己的 getSession()方法,接管创建和管理Session数据的工作。spring-session就是通过这样的思路实现的。


1、pom.xml依赖:

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>

2、spring.xml配置:

<!-- 将session放入redis -->
    <context:annotation-config/>
    <bean id="redisHttpSessionConfiguration"  class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
        <property name="maxInactiveIntervalInSeconds" value="120" />
     </bean>
    <bean
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!-- redis 配置 -->
        <property name="hostName" value="192.168.0.48" />
        <property name="port" value="6379" />
    </bean>


3、web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </context-param>
    <!-- 分布式Session共享Filter -->
    <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>


这样就可以实现分布式Session了。

注意:

1.spring的这个配置文件一定要写在web.xml的<context-param>部分,写在其他地方不行。
2.filter的名字必须是springSessionRepositoryFilter
3.如果使用了shiro,web.xml中一定要把<context-param>放在最前面,然后写shiro的Filter配置,再写spring-session的Filter配置。后面就是其他的编码和servlet配置了。


个人理解:由于spring-session 只是将原生的session以指定的方式进行存储,至于session的操作,它没有什么修改,无侵入,装饰者模式。理解的人自然理解,不理解的人就自行体会吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Session Data Redis 是 Spring Session 提供的一种基于 Redis 的分布式会话管理方案。它可以将用户的会话信息存储在 Redis 中,实现分布式场景下的会话共享。 使用 Spring Session Data Redis 需要进行以下步骤: 1. 添加 Redis 依赖 在项目的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> ``` 2. 配置 Redis 连接 在 Spring Boot 应用的配置文件中,添加 Redis 的连接配置: ```yaml spring: redis: host: localhost port: 6379 ``` 3. 启用 Spring Session 在 Spring Boot 应用的启动类上添加 @EnableRedisHttpSession 注解,启用 Spring Session: ```java @SpringBootApplication @EnableRedisHttpSession public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 使用 Spring Session 在需要获取当前用户的会话信息时,注入 HttpSession 对象即可: ```java @Controller public class MyController { @GetMapping("/hello") public String hello(HttpSession session) { String username = (String) session.getAttribute("username"); return "Hello, " + username + "!"; } } ``` Spring Session Data Redis 还支持一些高级特性,如会话过期时间、会话销毁监听器等。需要了解更多详情,请参考官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值