Redis实现,分布式Session共享

ops/2025/2/1 19:32:12/

依赖 不指定版本防止冲突 直接用版本依赖

 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.4</version><relativePath/> <!-- lookup parent from repository --></parent>
//直接使用依赖的版本<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis --><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency><dependencies>

 yml文件配置

 配置类 解决  Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' 报错 我解决了好长时间,后面有具体的redis配置细节

@Configuration
public class RedisHttpSessionConfig {/***  解决redis集群环境没有开启Keyspace notifications导致的**  Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource** */@Beanpublic static ConfigureRedisAction configureRedisAction() {return ConfigureRedisAction.NO_OP;}
}

启动两个客户端,8080端口 9000端口 调用接口 

8080端口请求登录 后端记录登陆状态 

9090端口拿到登陆状态 

 查看redis存储数据,redis实现session共享成功

 配置细节

redis文档 根目录下 redis.conf

1、文件将里面的daemonize no 改成 yes,让服务在后台启动

2、文件将里面的requirepass改成自己的密码,此密码为以后客户端验证身份使用

3、文件里面的bind,修改为指定的IP地址即为只允许该IP地址访问,注释即为允许所有主机访问

4、文件里面的protected-mode改为no,同意远程连接访问
5、
redis集群环境没有开启Keyspace notifications 上面配置类解决了这个问题 这里可以忽略

防火墙开启,开放redis端口或者其他服务的端口

关闭防火墙

systemctl stop firewalld

开启防火墙

systemctl start firewalld

开放指定端口

firewall-cmd --zone=public --add-port=6379/tcp --permanent

关闭指定接口

firewall-cmd --zone=public --remove-port=6379/tcp --permanent

重新加载防火墙配置

firewall-cmd --reload

业务代码实现接口

 /*** 用户登录** @param userLoginRequest* @param request* @return*/@PostMapping("/login")public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {if (userLoginRequest == null) {return ResultUtils.error(ErrorCode.PARAMS_ERROR);}String userAccount = userLoginRequest.getUserAccount();String userPassword = userLoginRequest.getUserPassword();if (StringUtils.isAnyBlank(userAccount, userPassword)) {return ResultUtils.error(ErrorCode.PARAMS_ERROR);}User user = userService.userLogin(userAccount, userPassword, request);return ResultUtils.success(user);}
实现类添加对session的保存 自动保存在redis
  /*** 用户登录** @param userAccount  用户账户* @param userPassword 用户密码* @param request* @return 脱敏后的用户信息*/@Overridepublic User userLogin(String userAccount, String userPassword, HttpServletRequest request) {.....// 4. 记录用户的登录态request.getSession().setAttribute(USER_LOGIN_STATE, safetyUser);return safetyUser;}


http://www.ppmy.cn/ops/154832.html

相关文章

Spring Boot 后端跨域解决方案:解锁前后端通信的障碍

随着前后端分离架构的普及&#xff0c;跨域资源共享&#xff08;Cross-Origin Resource Sharing, CORS&#xff09;问题成为了许多开发者必须面对的一个挑战。当Web浏览器尝试从一个源加载资源到另一个不同的源时&#xff0c;出于安全考虑&#xff0c;它会实施同源策略&#xf…

【Linux】日志设计模式与实现

&#x1f525; 个人主页&#xff1a;大耳朵土土垚 &#x1f525; 所属专栏&#xff1a;Linux系统编程 这里将会不定期更新有关Linux的内容&#xff0c;欢迎大家点赞&#xff0c;收藏&#xff0c;评论&#x1f973;&#x1f973;&#x1f389;&#x1f389;&#x1f389; 文章目…

关于hexo-deploy时Spawn-Failed的几种解决方案

title: 关于hexo deploy时Spawn Failed的几种解决方案 tags: 技术踩坑 abbrlink: 31824 date: 2023-08-20 11:42:13 前言 昨天晚上&#xff0c;我兴致勃勃的打开了cmd窗口&#xff0c;准备写一篇有关了解Kaggle的博客&#xff0c;当我文思泉涌&#xff0c;啪嗒啪嗒地码完整篇…

Java中的注解与反射:深入理解getAnnotation(Class<T> annotationClass)方法

Java的注解&#xff08;Annotation&#xff09;是一种元数据机制&#xff0c;它允许我们在代码中添加额外的信息&#xff0c;这些信息可以在编译时或运行时被读取和处理。结合Java的反射机制&#xff08;Reflection&#xff09;&#xff0c;我们可以在运行时动态地获取类、方法…

pytorch实现门控循环单元 (GRU)

人工智能例子汇总&#xff1a;AI常见的算法和例子-CSDN博客 特性GRULSTM计算效率更快&#xff0c;参数更少相对较慢&#xff0c;参数更多结构复杂度只有两个门&#xff08;更新门和重置门&#xff09;三个门&#xff08;输入门、遗忘门、输出门&#xff09;处理长时依赖一般适…

宝塔面板SSL加密访问设置教程

参考:https://www.bt.cn/bbs/thread-117246-1-1.html 如何快速使用证书加密访问面板 因早期默认未开启https访问所以没有相关的风险提醒&#xff0c;现面板默认已开启https加密访问、提升安全性 由于采用的是服务器内部本身签发证书&#xff0c;不被公网浏览器信任请参考以下步…

【JavaEE】-- 计算机是如何工作的

文章目录 1. 冯诺依曼体系&#xff08;VonNeumann Architecture)2. CPU 基本工作流程2.1 寄存器(Register)和 内存(RAM)2.2 控制单元 CU(ControlUnit)2.3 指令&#xff08;Instruction) 3. 操作系统&#xff08;OperatingSystem)3.1 操作系统的定位3.2 什么是进程/任务(Process…

Keepalived高可用集群企业应用实例二

一、实现ipvs的高可用性 ipvs相关配置 虚拟服务器配置结构&#xff1a; virtual_server ip port { …… real_server { …… } real_server { …… } } virtual server (虚拟服务器)的定义格式 virtual_server ip port 定义虚拟主机ip地址及其端口 virtual_server …