【SpringBoot】31 Session + Redis 实战

Gitee

https://gitee.com/Lin_DH/system

介绍

【SpringBoot】30 Cookie、Session、Token https://blog.youkuaiyun.com/weixin_44088274/article/details/144241595

背景

Spring Session 是 Spring 的一个子项目,它提供了一种管理用户会话信息的方法,无论是在单服务的应用程序中,还是在分布式系统中,都能运用。Spring Session 提供了一种机制,可以将应用服务器的会话(HttpSession)抽象化,使得开发者可以在不同的环境下使用系统的 API 来管理会话。

Spring Session

原理

Spring Session 使支持集群会话变得简单,无需绑定到特定应用程序容器的解决方案。

  • HttpSession:以通用的方式替代应用程序容器(如 Tomcat)中(Servlet 容器实现)的 HttpSession,实现了会话数据的外部存储,并支持在请求头(Header)中提供 sessionId,方便提供 RESTful API。
  • WebSocket:提供在接收 WebSocket 消息时保持 HttpSession 活跃的能力。
  • WebSession:允许以与应用程序容器无关的方式替换 Spring WebFlux 的 WebSession。
  • 配置类:提供了几个关键配置类,如 RedisHttpSessionConfiguration 和 SpringHttpSessionConfiguration,用于配置 Redis 作为会话数据的存储源。
  • SessionRepositoryFilter:SessionRepositoryFilter 是 Spring Session 的核心组件之一,它负责在请求处理之前和之后与会话存储进行交互,以确保会话数据的正确加载和保存。
    在这里插入图片描述

组成

Spring Session Core:提供核心的 Spring 会话功能和 Api。
Spring Session Data Redis:提供 SessionRepository 和 ReactiveSessionRepository 的实现,支持 Redis 和配置。
Spring Session JDBC:提供由关系型数据库和配置支持所支持的 SessionRepository 实现。
Spring Session Hazelcast:提供由 Hazelcast 和配置支持所支持的 SessionRepository 实现。

Redis 实现分布式 Session

在这里插入图片描述
在分布式系统中,通常会将 Session 存储在 Redis 中来实现分布式 Session,这样可以在多台服务器之间共享 Session 数据。实现分布式 Session 通常使用 Redis 的 Hash 结构。
1)用户登录:当用户登录成功后,服务端会生成一个唯一的 SessionId(通常是一串随机字符串,如 UUID)。
2)存储 Session:将用户的会话信息(如用户ID,权限信息等)与 SessionId 关联,并存储在 Redis 中,Redis 使用 Hash 结构来存储这些数据,其中 SessionID 作为 Hash 的 Key,用户的会话信息作为 Hash 的 Value。
3)设置 Cookie:将 SessionId 通过 Cookie 发送到客户端浏览器,客户端每次请求时都需要携带上 Cookie。
4)请求处理:在客户端每次发起请求时,服务端会检查 Cookie 中的 SessionId,服务端用该 SessionId 从 Redis 中检索对应的 Session 数据。
5)认证与授权:通过检索到的 Session 数据,服务端可以验证用户身份,并根据用户的会话信息进行授权。
Session 过期:设置 Session 过期时间,当用户长时间不活动或者显式退出登录时,服务端会从 Redis 中删除对应的 Session 数据。
在这里插入图片描述
在这里插入图片描述

代码实现

依赖

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

          <!-- 实现对 Spring Session 使用 Redis 作为数据源的自动化配置 -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

        <!-- 实现对 Spring Data Redis 的自动化配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

配置

application.yml

spring:
  redis:
    host: localhost
    port: 6379
#    password:
    #Redis数据库号,默认为0
    database:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值