zoukankan      html  css  js  c++  java
  • 【Spring】SpringBoot + 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、

        第一次访问响应, 设置了Cookie, Cookie的域就是 “localhost”, 路径为 “/”

        

        所有第二次访问 http://localhost:8081/hello, 会将此Cookie带到服务端去

        注: 也可以使用nginx进行负载均衡, 效果相同

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

      4) Redis存储结果

      

      注:key2:如果在session中存入自定义属性,那么也会保存在key2中

    1 request.getSession().setAttribute("uid", "xxxx001");

    二、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  

  • 相关阅读:
    将 SharePoint 2010 网站集升级到 2013 (含沙盒方案)
    几款网络云存储服务的使用对比
    技术发展飞快,一日十年
    项目背景介绍
    初次接触,简单的了解需求
    用色彩区分 SharePoint 2010 Calendar 的日历项
    嘿,我这里有一个 Survey!
    博客页面在 IE 浏览器中样式混乱了(已经更换了样式)
    关于 Graphviz
    搭建使用 RTX51Tiny 的 C51 Keil 项目环境
  • 原文地址:https://www.cnblogs.com/h--d/p/14857088.html
Copyright © 2011-2022 走看看