当前位置:网站首页>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); } }
边栏推荐
猜你喜欢
数据机构---第五章树与二叉树---二叉树的概念---应用题
【MySQL系列】 MySQL表的增删改查(进阶)
【MySQL系列】MySQL索引事务
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
Share an interface test project (very worth practicing)
【图像融合】基于加权和金字塔实现图像融合附matlab代码
12306抢票,极限并发带来的思考?
WEB安全基础 - - - XRAY使用
CDH6 Hue to open a "ASCII" codec can 't encode characters
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
随机推荐
检查 Oracle 版本的 7 种方法
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
C language - branch statement and loop statement
获取小猪民宿(短租)数据
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
@Scheduled注解详解
TexturePacker使用文档
Quartus uses tcl files to quickly configure pins
20220725 Information update
GetHashCode与Equals
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
学习笔记:机器学习之回归
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
recursion: method calls itself
Flink学习第三天——一文带你了解什么是Flink流?
6134. Find the closest node to the given two nodes - force double hundred code