SpringSecurity踢出指定用户

devtools/2025/3/1 12:34:40/

SpringSecurity中可以使用 SessionRegistry 的实现类 SessionRegistryImpl 来获取session相关信息,可以通过这个实现类来踢出用户。

SpringSecurity配置

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@AutowiredISysUserService userService;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/webjars/**","/asserts/**","/login").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").loginProcessingUrl("/loginPost").failureUrl("/login?error=true").defaultSuccessUrl("/index").and().logout().logoutUrl("/logout").addLogoutHandler(new MyLogoutHandler()).logoutSuccessUrl("/login").and().rememberMe().userDetailsService(userService).tokenRepository(jdbcTokenRepository())//保存登录状态时间,单位是秒.tokenValiditySeconds(60*60*3).and()//关闭请求头中的frame选项,不限制iframe.headers().frameOptions().disable()//关闭跨域.and().csrf().disable().sessionManagement()//无效session跳转.invalidSessionUrl("/login")//同时登陆多个只保留一个.maximumSessions(1)//过期session跳转.expiredUrl("/login").sessionRegistry(sessionRegistry());}/** 注册SessionRegistry*/@Beanpublic SessionRegistry sessionRegistry(){return new SessionRegistryImpl();}

控制器

/** 踢出用户 */@PreAuthorize("hasRole('管理员')")@GetMapping("/logout/{id}")@ResponseBodypublic String logout(@PathVariable Long id) throws NoSuchFieldException {//通过id查询用户SysUser sysUser = userService.selectUserByUserId(id);//获取所有principal信息List<Object> allPrincipals = sessionRegistry.getAllPrincipals();for (Object allPrincipal : allPrincipals) {User user=(User)allPrincipal;//判断是否跟传递的id所找到的用户登录名一致if(user.getUsername().equals(sysUser.getLoginName())){List<SessionInformation> allSessions = sessionRegistry.getAllSessions(allPrincipal, false);for (SessionInformation session : allSessions) {//使当前session过期session.expireNow();}}}return "ok";}

http://www.ppmy.cn/devtools/163616.html

相关文章

MR30系列分布式I/O:高稳定与高精准赋能锂电池覆膜工艺革新

在新能源行业高速发展的背景下&#xff0c;锂电池生产工艺对自动化控制的精准性和可靠性提出了更高要求。作为锂电池生产中的关键环节&#xff0c;覆膜工艺直接关系到电池的绝缘性能、安全性及使用寿命。面对复杂的工艺控制需求&#xff0c;明达技术MR30系列分布式I/O模块凭借其…

【Groovy】流程控制

1 选择结构 Groovy 中选择结构主要包含 if -else、switch 语句&#xff0c;并且可以返回结果。 1.1 if-else def score 85 if (score > 90) {println("优秀") } else if (score > 80) {println("良好") } else if (score > 60) {println("…

【每日刷题】Day184

【每日刷题】Day184 &#x1f955;个人主页&#xff1a;开敲&#x1f349; &#x1f525;所属专栏&#xff1a;每日刷题&#x1f34d; &#x1f33c;文章目录&#x1f33c; 1. 1700. 无法吃午餐的学生数量 - 力扣&#xff08;LeetCode&#xff09; 2. 146. LRU 缓存 - 力扣&a…

SQL Server 视图的更新排查及清除缓存

目录 前言排查方向 前言 获取数据的时候&#xff0c;发现数据少了两个字段值&#xff0c;归根原因是Java中的实体类少写了两个&#xff0c;后续补充上就好了&#xff01; 但也正了解到视图中的刷新原理以及排查机制&#xff0c;如果确认是视图等引起&#xff0c;可结合如下文…

【MyBatis】核心配置文件详解

文章目录 MyBatis核心配置文件详解1.configuration&#xff1a;2.environments&#xff1a;3.environmen&#xff1a;4.transactionManager&#xff1a;5.dataSource&#xff1a;5.1 UNPOOLED&#xff1a;5.2 POOLED&#xff1a;5.3 JNDI&#xff1a; 6. properties7. mapper M…

使用Fuse-DFS挂载文件存储 HDFS-后端存储ceph

1. 编译环境准备 yum install cmake3 ln -s /usr/bin/cmake3 /usr/bin/cmake yum install gcc-c安装挂载依赖 yum -y install fuse fuse-devel fuse-libs执行以下命令&#xff0c;载入FUSE模块 modprobe fuse2. 下载源码包 hadoop-3.3.4-src.tar.gz解压后执行以下命令 打开…

Transformer 代码剖析3 - 参数配置 (pytorch实现)

一、硬件环境配置模块 参考&#xff1a;项目代码 原代码实现 """ author : Hyunwoong when : 2019-10-22 homepage : https://github.com/gusdnd852 """ import torch # GPU device setting device torch.device("cuda:0" if tor…

网络变压器的主要电性参数与测试方法(2)

Hqst盈盛&#xff08;华强盛&#xff09;电子导读&#xff1a;网络变压器的主要电性参数与测试方法&#xff08;2&#xff09;.. 今天我们继续来看看网络变压器的2个主要电性参数与它的测试方法&#xff1a; 1. 线圈间分布电容Cp:线圈间杂散静电容 测试条件:100KHz/0.1…