security 会话并发管理

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 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);

   }

}


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
10月前
|
缓存 运维 监控
SSL Session默认设置导致线程阻塞了几十秒的案例分析
SSL Session默认设置导致线程阻塞了几十秒的案例分析
114 0
|
安全 Java Spring
Spring Security系列教程19--会话管理之处理会话过期
前言 在上一章节中,一一哥 给各位讲解了HTTP协议、会话、URL重新、会话固定攻击等概念,并且实现了对会话固定攻击的防御拦截。 在Spring Security中,其实除了可以对会话固定攻击进行拦截之外,还可以对会话过期进行处理,也就是会话可能会过期,过期了该怎么处理。接下来请各位跟着 壹哥 继续学习,看看会话过期时到底怎么处理的吧。 一. 会话过期 1. 会话过期概念 在处理会话过期之前,我们首先得知道啥是会话过期。 所谓的会话过期,是指当用户登录网站后,较长一段时间没有与服务器进行交互,将会导致服务器上的用户会话数据(即session)被销毁。此时,当用户再次操作网页时,如果服务器进
502 0
|
安全 数据安全/隐私保护
如何处理多个Yii2.0应用程序之间的SSO会话共享?
如何处理多个Yii2.0应用程序之间的SSO会话共享?
|
存储 缓存 数据安全/隐私保护
Jasny SSO是如何处理用户会话的?底层原理是什么?
Jasny SSO是如何处理用户会话的?底层原理是什么?
|
应用服务中间件 API
Web阶段:第十七章:Session会话
Web阶段:第十七章:Session会话
Web阶段:第十七章:Session会话
|
安全 Go API
第二十三章 CSP Session 管理 - 身份验证共享策略
第二十三章 CSP Session 管理 - 身份验证共享策略
|
存储 安全 Go
第十八章 CSP Session 管理 - 与 CSP.Session 的Sessions
第十八章 CSP Session 管理 - 与 CSP.Session 的Sessions
|
存储 Go 数据库
第二十章 CSP Session 管理 - 状态管理
第二十章 CSP Session 管理 - 状态管理
|
负载均衡 网络协议 开发者
配置会话保持|学习笔记
快速学习配置会话保持
326 0
配置会话保持|学习笔记
|
Web App开发 安全 Java
Spring Security系列教程20--会话管理之会话并发控制
前言 现在我们已经掌握了如何防御会话固定攻击,以及在会话过期时的处理策略,但是这些都是针对单个HttpSession来说的,对于会话来说,我们还有另一种情况需要考虑:会话并发控制! 那什么是会话并发控制呢?假如我想实现 “在我们的网站中,同一时刻只允许一个用户登录” 这样的效果,该怎么做? 请各位带着以上的这些问题,跟着 一一哥 继续往下学习吧。 一. 会话并发控制 1. 会话并发控制 首先我们来了解一下会话并发控制的概念。 有时候出于安全的目的,我们可能会有这样的需求,就是规定在同一个系统中,只允许一个用户在一个终端上登录,这其实就是对会话的并发控制。 2. 并发控制实现思路 如果我们
437 0