当前位置:网站首页>security 会话并发管理
security 会话并发管理
2022-08-01 23:52:00 【程序三两行】
一、简介
会话指得是浏览器和服务端通过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);
}
}边栏推荐
- FAST-LIO2代码解析(二)
- numpy.where
- 中职网络安全竞赛B7比赛部署流程
- Convert LocalDateTime to Date type
- 在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
- Chapter 19 Tips and Traps: Common Goofs for Novices
- Flink Yarn Per Job - 提交流程一
- 仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)
- Share an interface test project (very worth practicing)
- 【Leetcode】1206. Design Skiplist
猜你喜欢
随机推荐
UI自动化测试框架搭建-标记性能较差用例
斜堆、、、
Chrome书签插件,让你实现高效整理
6132. 使数组中所有元素都等于零-快速排序法
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
Bean的生命周期
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
Spark Sql之union
很多人喜欢用多御安全浏览器,竟是因为这些原因
Classical Literature Reading--DLO
windows sql server 如何卸载干净?
伸展树的特性及实现
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
颜色透明参数
Several interview questions about golang concurrency
color transparency parameter
在MySQL中使用MD5加密【入门体验】
Use Jenkins for continuous integration, this knowledge point must be mastered
【ACWing】406. 放置机器人

![[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环](/img/63/16de443caf28644d79dc6e6889e5dd.png)







