当前位置:网站首页>security session concurrency management
security session concurrency management
2022-08-02 00:06:00 【Three or two lines of program】
一、简介
Session refers to the connection between the browser and the serversession交互过程
二、会话并发管理
1、What is session concurrency
当前系统中,Whether the same user can log in on multiple devices,springsecurity默认没有限制,You can log in on multiple devices,可以在springsecurity中配置管理
2、代码
引入security不做任何配置 By default, the same account can log in to access the system in multiple browsers
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .csrf().disable() .sessionManagement()//开启会话管理 .maximumSessions(1);//The same account can only be logged in in one browser } /** *找个bean可以不加,但是建议加上 * security提供一个mapcome to protect the currenthttp session记录 Implement session concurrency management,Add one when logging in ,Removes one from the collection on exit */ @Bean public HttpSessionEventPublisher httpSessionEventPublisher(){ return new HttpSessionEventPublisher(); } }
The following prompt appears when multiple browsers are logged in
This session has been expired (possibly due to multiple concurrent logins being attempted as the same user).
Session invalidation how can we change to find a hint?
3、Handles when a session is pushed offline
3.1、传统web开发
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .csrf().disable() .sessionManagement() .maximumSessions(1) .expiredUrl("/login");//Jump address when being squeezed offline } @Bean public HttpSessionEventPublisher httpSessionEventPublisher(){ return new HttpSessionEventPublisher(); } }
3.2、前后端分离
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .csrf().disable() .sessionManagement() .maximumSessions(1) .expiredSessionStrategy(event -> { HttpServletResponse response = event.getResponse(); Map<String,Object> map = new HashMap<>(); map.put("code",500); map.put("msg","The current account is logged in from different places"); String result = new ObjectMapper().writeValueAsString(map); response.setContentType("application/json;charset=UTF-8"); response.getWriter().println(result); response.flushBuffer(); });//A parameter is a functional interface 直接用lambda处理 } @Bean public HttpSessionEventPublisher httpSessionEventPublisher(){ return new HttpSessionEventPublisher(); } }
4、禁止再次登录
The default is to be squeezed offline You can set latecomers to be unable to log in
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .csrf().disable() .sessionManagement() .maximumSessions(1) .expiredUrl("/login") .maxSessionsPreventsLogin(true);//一旦登录 禁止再次登录 } @Bean public HttpSessionEventPublisher httpSessionEventPublisher(){ return new HttpSessionEventPublisher(); } }
5、Distributed session sharing
The above sessions are all passed in memorymap集中管理,Therefore, it cannot be shared in a distributed cluster system,To be used in the cluster,就要用spring-session集合redis实现session共享
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
System profile configurationredis
spring.redis.port=6379
spring.redis.url=localhost
security配置
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //注入session管理方案 @Autowired private FindByIndexNameSessionRepository findByIndexNameSessionRepository; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .and() .csrf().disable() .sessionManagement() .maximumSessions(1) .expiredUrl("/login") .sessionRegistry(sessionRegistry())//将sessionWho to manage .maxSessionsPreventsLogin(true); } /** * 创建session 同步到redis的方案 */ @Bean public SpringSessionBackedSessionRegistry sessionRegistry(){ return new SpringSessionBackedSessionRegistry(findByIndexNameSessionRepository); } }
边栏推荐
- Spark Sql之join on and和where
- An interview question about iota in golang
- 类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
- easy-excel 解决百万数据导入导出,性能很强
- Secondary Vocational Network Security Competition B7 Competition Deployment Process
- yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
- recursion: method calls itself
- Quartus 使用 tcl 文件快速配置管脚
- Flink Yarn Per Job - Yarn应用
- [LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
猜你喜欢
Secondary Vocational Network Security Competition B7 Competition Deployment Process
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
中职网络安全竞赛B7比赛部署流程
background-image使用
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
架构基本概念和架构本质
Get piggy homestay (short-term rental) data
WEB安全基础 - - - XRAY使用
随机推荐
使用 Zadig 交付云原生微服务应用
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
Flink学习第三天——一文带你了解什么是Flink流?
windows sql server 如何卸载干净?
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
DOM 事件及事件委托
伸展树的特性及实现
Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
TCP 可靠吗?为什么?
recursion: method calls itself
LocalDateTime转为Date类型
UI自动化测试框架搭建-标记性能较差用例
Loading configuration of Nacos configuration center
尚硅谷MySQL学习笔记
Spark Sql之join on and和where
SphereEx苗立尧:云原生架构下的Database Mesh研发实践