diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysRoleController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysRoleController.java index 88d3a1e6..6723dc2d 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysRoleController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysRoleController.java @@ -1,10 +1,6 @@ package com.ruoyi.system.controller; 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.CacheConstants; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.web.controller.BaseController; import com.ruoyi.common.excel.utils.ExcelUtil; @@ -12,11 +8,9 @@ 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.system.api.domain.SysDept; import com.ruoyi.system.api.domain.SysRole; import com.ruoyi.system.api.domain.SysUser; -import com.ruoyi.system.api.model.LoginUser; import com.ruoyi.system.domain.SysUserRole; import com.ruoyi.system.service.ISysDeptService; import com.ruoyi.system.service.ISysPermissionService; @@ -110,25 +104,7 @@ public class SysRoleController extends BaseController { return R.fail("修改角色'" + role.getRoleName() + "'失败,角色权限已存在"); } if (roleService.updateRole(role) > 0) { - List keys = StpUtil.searchTokenValue("", 0, -1, false); - if (CollUtil.isEmpty(keys)) { - return R.ok(); - } - // 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作 - keys.parallelStream().forEach(key -> { - String token = key.replace(CacheConstants.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) { - } - } - }); + roleService.cleanOnlineUserByRole(role.getRoleId()); return R.ok(); } return R.fail("修改角色'" + role.getRoleName() + "'失败,请联系管理员"); diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java index 1c68a918..18cffdd3 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java @@ -174,4 +174,7 @@ public interface ISysRoleService { * @return 结果 */ int insertAuthUsers(Long roleId, Long[] userIds); + + void cleanOnlineUserByRole(Long roleId); + } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java index 303ad01f..9c29ea49 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java @@ -1,5 +1,7 @@ package com.ruoyi.system.service.impl; +import cn.dev33.satoken.exception.NotLoginException; +import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.Wrapper; @@ -14,6 +16,7 @@ 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.system.api.domain.SysRole; +import com.ruoyi.system.api.model.LoginUser; import com.ruoyi.system.domain.SysRoleDept; import com.ruoyi.system.domain.SysRoleMenu; import com.ruoyi.system.domain.SysUserRole; @@ -361,9 +364,13 @@ public class SysRoleServiceImpl implements ISysRoleService { */ @Override public int deleteAuthUser(SysUserRole userRole) { - return userRoleMapper.delete(new LambdaQueryWrapper() + int rows = userRoleMapper.delete(new LambdaQueryWrapper() .eq(SysUserRole::getRoleId, userRole.getRoleId()) .eq(SysUserRole::getUserId, userRole.getUserId())); + if (rows > 0) { + cleanOnlineUserByRole(userRole.getRoleId()); + } + return rows; } /** @@ -375,9 +382,13 @@ public class SysRoleServiceImpl implements ISysRoleService { */ @Override public int deleteAuthUsers(Long roleId, Long[] userIds) { - return userRoleMapper.delete(new LambdaQueryWrapper() + int rows = userRoleMapper.delete(new LambdaQueryWrapper() .eq(SysUserRole::getRoleId, roleId) .in(SysUserRole::getUserId, Arrays.asList(userIds))); + if (rows > 0) { + cleanOnlineUserByRole(roleId); + } + return rows; } /** @@ -401,6 +412,33 @@ public class SysRoleServiceImpl implements ISysRoleService { if (list.size() > 0) { rows = userRoleMapper.insertBatch(list) ? list.size() : 0; } + if (rows > 0) { + cleanOnlineUserByRole(roleId); + } return rows; } + + @Override + public void cleanOnlineUserByRole(Long roleId) { + List keys = StpUtil.searchTokenValue("", 0, -1, false); + if (CollUtil.isEmpty(keys)) { + return; + } + // 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作 + keys.parallelStream().forEach(key -> { + String token = StringUtils.substringAfterLast(key, ":"); + // 如果已经过期则跳过 + if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) { + return; + } + LoginUser loginUser = LoginHelper.getLoginUser(token); + if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) { + try { + StpUtil.logoutByTokenValue(token); + } catch (NotLoginException ignored) { + } + } + }); + } + }