内容简介:Spring Security源码分析九:Spring Security Session管理
Session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。Session 对象最常见的一个用法就是存储用户的首选项。
Session管理
本文主要描述在 Spring Security
下 Session
的以下三种管理,
-
Session
超时时间 -
Session
的并发策略 -
集群环境
Session
处理
Session超时
-
application.yml
配置超时时间
server: port: 80 session: timeout: 60
http. ...... .sessionManagement() .invalidSessionUrl("/session/invalid")//session失效跳转的链接 .....
-
Cotroller
中/session/invalid
@GetMapping("/session/invalid") @ResponseStatus(code = HttpStatus.UNAUTHORIZED) public Result<String> sessionInvalid() { return ResultUtil.error(HttpStatus.UNAUTHORIZED.value(), "session失效"); }
效果如下:
Session的并发策略
http. ...... .maximumSessions(1)//最大session并发数量1 .maxSessionsPreventsLogin(false)//false之后登录踢掉之前登录,true则不允许之后登录 .expiredSessionStrategy(new MerryyounExpiredSessionStrategy())//登录被踢掉时的自定义操作 .....
-
MerryyounExpiredSessionStrategy
@Slf4j public class MerryyounExpiredSessionStrategy implements SessionInformationExpiredStrategy { @Override public void onExpiredSessionDetected(SessionInformationExpiredEvent eventØ) throws IOException, ServletException { eventØ.getResponse().setContentType("application/json;charset=UTF-8"); eventØ.getResponse().getWriter().write("并发登录!"); } }
效果如下:
当 maxSessionsPreventsLogin(true)
可参考: Spring-Security
和 security-oauth2
集群环境Session处理
- 添加spring-session-data-redis依赖
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.3.1.RELEASE</version> </dependency>
- 配置Spring-session存储策略
spring: redis: host: localhost port: 6379 session: store-type: redis
-
测试
8080
和8081
端口分别启动项目
java -jar spring-security.jar --server.port=8080 java -jar spring-security.jar --server.port=8081
效果如下:
关于更多Spring Session可参考:程序猿DD
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 以太坊源码分析(36)ethdb源码分析
- [源码分析] kubelet源码分析(一)之 NewKubeletCommand
- libmodbus源码分析(3)从机(服务端)功能源码分析
- [源码分析] nfs-client-provisioner源码分析
- [源码分析] kubelet源码分析(三)之 Pod的创建
- Spring事务源码分析专题(一)JdbcTemplate使用及源码分析
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Automate This
Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95
"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!