SpringSpringBoot + SpringSession + Redis 实现Session共享

Posted H__D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringSpringBoot + SpringSession + Redis 实现Session共享相关的知识,希望对你有一定的参考价值。

  本章介绍在SpringBoot项目中,使用 spring-session-data-redis.jar 实现Session共享

  SpringBoot与Redis整合参考:【SpringBoot】SpringBoot 整合Redis

一、使用 spring-session-data-redis

1、搭建SpringBoot Web项目

  引入redis依赖和 spring-session-data-redis 依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.test.redis</groupId>
 8     <artifactId>test-springboot-redis-session</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.2.5.RELEASE</version>
15     </parent>
16 
17     <properties>
18         <maven.compiler.source>8</maven.compiler.source>
19         <maven.compiler.target>8</maven.compiler.target>
20     </properties>
21 
22 
23     <dependencies>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28 
29         <!-- SpringBoot整合redis -->
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-data-redis</artifactId>
33         </dependency>
34 
35         <dependency>
36             <groupId>org.springframework.session</groupId>
37             <artifactId>spring-session-data-redis</artifactId>
38             <version>2.2.1.RELEASE</version>
39         </dependency>
40 
41 
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-test</artifactId>
45             <scope>test</scope>
46         </dependency>
47 
48     </dependencies>
49 
50 </project>

2、配置application.yml文件

 1 # 项目端口
 2 server:
 3   port: 8080
 4 
 5 spring:
 6   redis:
 7     database: 0
 8     timeout: 3000
 9     password: 123456
10     # 单节点模式
11     host: 127.0.0.1
12     port: 6379
View Code

3、编写启动类与controller

  1) Application.java

 1 // @EnableRedisHttpSession 可以加也可以不加,
 2 // RedisHttpSessionConfiguration会被自动导入
 3 @SpringBootApplication
 4 public class Application {
 5 
 6     /**
 7      * spring-session-data-redis Session共享
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         SpringApplication.run(Application.class);
12     }
13 }

  2) HelloController.java

 1 @Controller
 2 public class HelloController {
 3 
 4     @RequestMapping("/hello")
 5     public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
 6         String sessionId = request.getSession().getId();
 7         System.out.println("sessionId = " + sessionId);
 8         response.getWriter().println("sessionId = " + sessionId);
 9     }
10 }

4、测试运行

  1) 分别使用8080、8081端口启动项

  2) 使用同一浏览器访问地址 http://localhost:8080/hello、http://localhost:8081/hello

  3) 2个地址的返回值 sessionId 应该是相同的

二、spring-session-data-redis 原理

1、原理图

  

2、分析

  1) SpringBoot项目启动,  spring.factories 引入了 SessionAutoConfiguration , 导致 RedisHttpSessionConfiguration、SpringHttpSessionConfiguration 两个配置类自动导入
   如果是Spring项目,就需要使用注解 @EnableRedisHttpSession 来开启RedisHttpSession

  2) RedisHttpSessionConfiguration 主要是配置了 RedisIndexedSessionRepository Session仓库类

  3) SpringHttpSessionConfiguration 主要是配置了 SessionRepositoryFilter Session仓库过滤器

  4) 通过 SessionRepositoryFilter 过滤器对请求进行拦截, 进行逻辑判断, 将 session 保持到 redis中

 

参考: 

  https://blog.csdn.net/shenchaohao12321/article/details/89473591

  https://blog.csdn.net/qq_27088383/article/details/107767000  

 

以上是关于SpringSpringBoot + SpringSession + Redis 实现Session共享的主要内容,如果未能解决你的问题,请参考以下文章

springspringboot定时任务

# SpringSpringBoot 开发实用技巧

SpringSpringBoot集成邮箱发送

SpringSpringboot常用注解:@Qualifier(不定时更新)

SpringSpringBoot2.6.4整合Swagger3.0.0填坑

SpringSpringBoot 2.6.4配置跨域