SpringBoot+Session+redis实现分布式登录

SpringBoot+Session+Redis实现分布式登录功能实现

文章目录

目录

文章目录

前言

一、引库

二、修改配置文件

三、使用

四、解决乱码问题

1.引库

2.配置redis序列化

3.配置Session-Redis序列化



前言

这里简单介绍一下,如果你想多台机器部署你的项目的话,在登录方面如果你用到了session的话,因为session中的登录信息是存放到内存中的,多机部署的话,假设你在A机器登录了,但是获取信息的时候查到了B机器,可是B机器根本就没有你的登录信息就会导致抛给用户,让用户重新登陆,可是如果有100台机器呢,意味着要登陆100遍吗,不合适,所以就需要把用户的登录信息存储下来,有什么方式吗?

  1. 使用Mysql数据库
  2. 使用Redis缓存

这里就考虑一个问题了,如果说登陆的用户信息太多的话,Mysql数据库会不会影响查询速度,也就是Qps,所以这个时候redis就可以完美的避开这种影响


一、引库

这里我用的springBoot版本是2.6.4,所以下面的两个库我选择了相同版本,如果你的项目用到了别的SpringBoot版本的话更换版本就可以

XML 复制代码
        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.6.4</version>
        </dependency>

        <!--session-redis-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>2.6.4</version>
        </dependency>

二、修改配置文件

XML 复制代码
  # redis配置
spring:
  redis:
    port: 6379
    host: localhost
    database: 0
  # session 失效时间(分钟)
  session:
    timeout: 86400
    store-type: redis

其实这里主要就是store-type设置成为redis就好

三、使用

这样的话你登录之后用户信息就会存储到redis中了

但是此时就出来了一个问题,那就是乱码了,接下来解决它

四、解决乱码问题

1.引库

XML 复制代码
          <!--Jackson JSON-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

2.配置redis序列化

java 复制代码
package com.yupi.xinggui.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        // 使用GenericJackson2JsonRedisSerializer来序列化和反序列化redis的value值
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.afterPropertiesSet();
        return template;
    }
}

3.配置Session-Redis序列化

java 复制代码
package com.yupi.xinggui.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession
public class SessionConfig {

    /**
     * 配置Spring Session的默认Redis序列化器。
     * 
     * 本方法旨在提供一个自定义的Redis序列化器,用于在Spring Session中序列化和反序列化会话数据。
     * 选择GenericJackson2JsonRedisSerializer是因为它支持将Java对象序列化为JSON格式,
     * 这样可以在Redis中存储更复杂的会话属性,而不仅仅是简单的字符串或数字。
     * 
     * @return GenericJackson2JsonRedisSerializer 实例,用于序列化和反序列化会话数据。
     */
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

}

现在就可以看到用户信息了

今日时间2024年7月20日,希望这篇博客可以帮助到你,愿你天天开心

相关推荐
独行侠影a7 小时前
APScheduler+Redis 分布式定时任务:解决多实例任务重复执行
数据库·redis·分布式
香菜TTT10 小时前
Kafka_深度解析_从架构原理到生产实践
分布式·架构·kafka·linq
paopaokaka_luck11 小时前
基于springboot3+vue3的智能文库平台(AI智能搜索、AI智能汇总、实时在线状态展示、多格式文档预览与富文本编辑、Echarts图形化分析)
前端·网络·spring boot·网络协议·echarts
zzzll111111 小时前
Spring Boot 入门指南:从零开始构建 Java Web 应用
java·前端·spring boot
不在逃避q12 小时前
Golang笔记之Redis
redis·笔记·golang
zcmodeltech15 小时前
智能矿井沙盘模型多系统协同控制系统设计:基于STM32与Modbus RTU的感知-传输-控制一体化方案
服务器·数据库·分布式·stm32·单片机·嵌入式硬件
江晓鱼未暖16 小时前
十七、Redis 核心原理与架构详解
大数据·数据库·数据仓库·redis·缓存·架构
红莲凪是16 小时前
Redis有哪些部署方案?了解哨兵机制吗?
java·redis·eureka
wWYy.16 小时前
基于Raft分布式Kv存储:RPC调用
分布式·rpc
小罗水16 小时前
第13章 Redis 缓存、幂等锁与任务状态
数据库·redis·缓存