SpringSecurity-12-Redis实现基于session共享登录方案
source link: https://blog.51cto.com/u_12023894/5166042
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SpringSecurity-12-Redis实现基于session共享登录方案
如果我们使用单机版本的Session存储身份信息的时候,如果服务器挂掉,那么服务就无法使用了。如果我们将项目部署到CD两台服务器上,Session就无法保持一致。如果用户user第一次访问C服务器,然后再次请求的时候访问D服务器,但是B没有存储user的session,这样用户就需要重新登录。
所以为了解决这种情况,我们可以将session放到redis中,以后用户请求都可以从redis中获取session,从而保持登录二点一致性。
Redis 安装
Windows下安装
下载地址:https://github.com/tporadowski/redis/releases
redis支持windows的32位和64位,我们根据系统情况进行下载,然后解压。
解压后打开文件夹,双击redis-server.exe打开redis服务,双击redis-cli.exe打开客户端
集成Spring session
引入maven依赖
在spring-session-redis和redis的maven依赖在项目的pom.xml中
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml 配置 redis
session:
timeout: 1m
store-type: redis
redis:
database: 0
port: 6379
启动项目, 进行登录
启动项目,进行登录,然后在redis的客户端输入**keys ***查看保存的session信息
- 默认情况下, 浏览器的 Cookie 中保存 SessionID 名称是 JSESSIONID
- 但是再使用redis的时候保存session的信息, 浏览器的 Cookie 中保存 SessionID 名称是 SESSION
这样就是再 session失效后,我们只是清除了JSESSIONID
,当再次请求的时候,都不会再进行登录。
我们统一在浏览器中将Cookie保存的SessionID名称设置为JSEESIONID
port: 8888
servlet:
session:
cookie:
name: JSESSIONID
重启项目,再次进行登录查看cookie保存的SessionID
如果您觉得本文不错,欢迎关注,点赞,收藏支持,您的关注是我坚持的动力!
原创不易,转载请注明出处,感谢支持!如果本文对您有用,欢迎转发分享!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK