security 会话并发管理

简介: security 会话并发管理

一、简介

会话指得是浏览器和服务端通过session交互过程

二、会话并发管理

1、什么是会话并发

当前系统中,同一个用户是否可以在多台设备登录,springsecurity默认没有限制,可以在多台设备登录,可以在springsecurity中配置管理

2、代码

引入security不做任何配置 默认同一个账号是可以在多个浏览器登录访问系统

@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);//同一个账号只能在一个浏览器登录

   }


   /**

    *找个bean可以不加,但是建议加上

    * security提供一个map来集护当前http session记录 实现会话并发管理,当登录时候增加一条 ,退出时从集合移除一个

    */

   @Bean

   public HttpSessionEventPublisher httpSessionEventPublisher(){

       return new HttpSessionEventPublisher();

   }

}

当多个浏览器登录时候出现如下提示

This session has been expired (possibly due to multiple concurrent logins being attempted as the same user).

会话失效我们该如何改变找个提示?

3、会话被挤下线时处理

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");//被挤下线时候跳转地址

   }

   @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","当前账号异地登录");

                   String result = new ObjectMapper().writeValueAsString(map);

                   response.setContentType("application/json;charset=UTF-8");

                   response.getWriter().println(result);

                   response.flushBuffer();

               });//参数是个函数式接口 直接用lambda处理

   }

   @Bean

   public HttpSessionEventPublisher httpSessionEventPublisher(){

       return new HttpSessionEventPublisher();

   }

}

4、禁止再次登录

默认是被挤下线方式 可以设置后来者无法登录

@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、分布式会话共享

上面会话都是通过内存中的map集中管理,所以无法在分布式集群系统中共享,要在集群中使用,就要用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>

系统配置文件配置redis

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())//将session交给谁管理

               .maxSessionsPreventsLogin(true);

   }


   /**

    * 创建session 同步到redis的方案

    */

   @Bean

   public SpringSessionBackedSessionRegistry sessionRegistry(){

       return new SpringSessionBackedSessionRegistry(findByIndexNameSessionRepository);

   }

}


相关文章
|
安全 C++
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
2248 0
|
存储 Java 应用服务中间件
IntelliJ IDEA - 远程 remote debug 教程实战和要点总结
IntelliJ IDEA - 远程 remote debug 教程实战和要点总结
5273 0
IntelliJ IDEA - 远程 remote debug 教程实战和要点总结
|
11月前
|
机器学习/深度学习 人工智能 算法
基于 YOLOv8 的智能杂草检测识别实战 [目标检测完整源码]
基于YOLOv8的智能杂草检测系统,融合目标检测与PyQt5可视化,实现田间杂草精准识别。涵盖数据标注、模型训练、推理部署全流程,支持图片、视频及实时摄像头检测,具备高精度、强鲁棒性与工程落地价值,适用于智慧农业、科研教学等场景,提供完整源码与预训练模型,助力AI+农业应用开发。
513 3
基于 YOLOv8 的智能杂草检测识别实战 [目标检测完整源码]
|
11月前
|
SQL 存储 Ubuntu
SQL Server 2025 CU1 (2026 年 1 月 累计更新) 现已正式支持 REHL 10 和 Ubuntu 24.04 最新的 Linux 发行版
SQL Server 2025 CU1 (2026 年 1 月 累计更新) 现已正式支持 REHL 10 和 Ubuntu 24.04 最新的 Linux 发行版
398 1
SQL Server 2025 CU1 (2026 年 1 月 累计更新) 现已正式支持 REHL 10 和 Ubuntu 24.04 最新的 Linux 发行版
|
监控 Java Maven
《服务治理》容错机制详解与实践
容错机制是分布式系统的核心,通过熔断、重试、降级等策略,在部分组件故障时保障系统可用性。本文系统介绍了Resilience4j实战、智能决策、监控告警及生产最佳实践,助力构建高韧性应用。
|
机器学习/深度学习 编解码 监控
目标检测实战(六): 使用YOLOv8完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)
这篇文章详细介绍了如何使用YOLOv8进行目标检测任务,包括环境搭建、数据准备、模型训练、验证测试以及模型转换等完整流程。
30917 59
目标检测实战(六): 使用YOLOv8完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)
基本时间单位 | 带你读《5G 空口设计与实践进阶 》之十五
为提供精确、一致的时间度量,NR 定义了最小时间单位 Tc。
基本时间单位 | 带你读《5G 空口设计与实践进阶 》之十五
|
存储 SQL 安全
使用 BurpSuite 基于 Token 机制实施
使用 BurpSuite 基于 Token 机制实施
使用 BurpSuite 基于 Token 机制实施
|
云安全 存储 安全
带你读《阿里云安全白皮书》(二十二)——云上安全重要支柱(16)
在全球化背景下,阿里云高度重视云平台的安全合规建设,确保客户在不同地区和行业能够满足监管要求。阿里云通过140多项安全合规认证,提供全面的专业安全合规服务和便捷高效的安全合规产品,帮助企业高效且低成本地实现安全合规目标。更多详情可参见阿里云官网“阿里云信任中心 - 阿里云合规”。