From 0ea206dfe19899499ff542fc2e521e54753bc269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Fri, 17 Feb 2023 14:13:22 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E8=A7=92=E8=89=B2=E5=90=8E=E8=B8=A2=E6=8E=89=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E5=85=B3=E7=9A=84=E7=99=BB=E5=BD=95=E7=94=A8?= =?UTF-8?q?=E6=88=B7=20=E7=94=A8=E6=88=B7=E9=87=8F=E8=BF=87=E5=A4=A7?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4redis=E9=98=BB=E5=A1=9E=E5=8D=A1?= =?UTF-8?q?=E9=A1=BF(=E5=BA=94=E7=B2=89=E4=B8=9D=E8=A6=81=E6=B1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/satoken/utils/LoginHelper.java | 7 ++++ .../controller/system/SysRoleController.java | 33 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java b/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java index 3bff1f26..1bc41976 100644 --- a/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java +++ b/ruoyi-common/ruoyi-common-satoken/src/main/java/com/ruoyi/common/satoken/utils/LoginHelper.java @@ -71,6 +71,13 @@ public class LoginHelper { return loginUser; } + /** + * 获取用户基于token + */ + public static LoginUser getLoginUser(String token) { + return ((JSONObject) StpUtil.getExtra(token, LOGIN_USER_KEY)).toBean(LoginUser.class); + } + /** * 获取用户id */ diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysRoleController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysRoleController.java index a3cf5a0b..91fbdeb8 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysRoleController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/system/SysRoleController.java @@ -1,13 +1,19 @@ package com.ruoyi.system.controller.system; import cn.dev33.satoken.annotation.SaCheckPermission; +import cn.dev33.satoken.exception.NotLoginException; +import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.collection.CollUtil; +import com.ruoyi.common.core.constant.GlobalConstants; import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.excel.utils.ExcelUtil; import com.ruoyi.common.log.annotation.Log; import com.ruoyi.common.log.enums.BusinessType; import com.ruoyi.common.mybatis.core.page.PageQuery; import com.ruoyi.common.mybatis.core.page.TableDataInfo; +import com.ruoyi.common.satoken.utils.LoginHelper; import com.ruoyi.common.web.core.BaseController; import com.ruoyi.system.domain.SysDept; import com.ruoyi.system.domain.SysUserRole; @@ -107,14 +113,25 @@ public class SysRoleController extends BaseController { } if (roleService.updateRole(role) > 0) { -// // 更新缓存用户权限 -// LoginUser loginUser = LoginHelper.getLoginUser(); -// SysUserVo sysUser = userService.selectUserById(loginUser.getUserId()); -// if (ObjectUtil.isNotNull(sysUser)) { -// loginUser.setMenuPermission(permissionService.getMenuPermission(sysUser.getUserId())); -// LoginHelper.setLoginUser(loginUser); -// } - // todo LoginUser 改为存储到token内部 无法热更新 等待后续想办法 + List keys = StpUtil.searchTokenValue("", 0, -1, false); + if (CollUtil.isEmpty(keys)) { + return R.ok(); + } + // 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作 + keys.parallelStream().forEach(key -> { + String token = key.replace(GlobalConstants.LOGIN_TOKEN_KEY, ""); + // 如果已经过期则跳过 + if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) { + return; + } + LoginUser loginUser = LoginHelper.getLoginUser(token); + if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(role.getRoleId()))) { + try { + StpUtil.logoutByTokenValue(token); + } catch (NotLoginException ignored) { + } + } + }); return R.ok(); } return R.fail("修改角色'" + role.getRoleName() + "'失败,请联系管理员");