From 9d8b9fabbea813e980d7f8e67ee633a55d205955 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 16 一月 2025 11:50:19 +0800
Subject: [PATCH] update 优化 ws模块替换session的时候关闭session连接
---
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java | 66 +++++++++++++++++++++++++++------
1 files changed, 54 insertions(+), 12 deletions(-)
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java
index 31f9498..0a2e485 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java
@@ -12,10 +12,12 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
+import org.dromara.common.core.constant.CacheNames;
+import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.constant.TenantConstants;
-import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
+import org.dromara.common.core.service.RoleService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
@@ -33,6 +35,7 @@
import org.dromara.system.mapper.SysRoleMenuMapper;
import org.dromara.system.mapper.SysUserRoleMapper;
import org.dromara.system.service.ISysRoleService;
+import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -45,7 +48,7 @@
*/
@RequiredArgsConstructor
@Service
-public class SysRoleServiceImpl implements ISysRoleService {
+public class SysRoleServiceImpl implements ISysRoleService, RoleService {
private final SysRoleMapper baseMapper;
private final SysRoleMenuMapper roleMenuMapper;
@@ -72,7 +75,7 @@
private Wrapper<SysRole> buildQueryWrapper(SysRoleBo bo) {
Map<String, Object> params = bo.getParams();
QueryWrapper<SysRole> wrapper = Wrappers.query();
- wrapper.eq("r.del_flag", UserConstants.ROLE_NORMAL)
+ wrapper.eq("r.del_flag", SystemConstants.NORMAL)
.eq(ObjectUtil.isNotNull(bo.getRoleId()), "r.role_id", bo.getRoleId())
.like(StringUtils.isNotBlank(bo.getRoleName()), "r.role_name", bo.getRoleName())
.eq(StringUtils.isNotBlank(bo.getStatus()), "r.status", bo.getStatus())
@@ -174,7 +177,7 @@
@Override
public List<SysRoleVo> selectRoleByIds(List<Long> roleIds) {
return baseMapper.selectRoleList(new QueryWrapper<SysRole>()
- .eq("r.status", UserConstants.ROLE_NORMAL)
+ .eq("r.status", SystemConstants.NORMAL)
.in(CollUtil.isNotEmpty(roleIds), "r.role_id", roleIds));
}
@@ -293,6 +296,10 @@
@Transactional(rollbackFor = Exception.class)
public int updateRole(SysRoleBo bo) {
SysRole role = MapstructUtils.convert(bo, SysRole.class);
+
+ if (SystemConstants.DISABLE.equals(role.getStatus()) && this.countUserRoleByRoleId(role.getRoleId()) > 0) {
+ throw new ServiceException("瑙掕壊宸插垎閰嶏紝涓嶈兘绂佺敤!");
+ }
// 淇敼瑙掕壊淇℃伅
baseMapper.updateById(role);
// 鍒犻櫎瑙掕壊涓庤彍鍗曞叧鑱�
@@ -309,7 +316,7 @@
*/
@Override
public int updateRoleStatus(Long roleId, String status) {
- if (UserConstants.ROLE_DISABLE.equals(status) && this.countUserRoleByRoleId(roleId) > 0) {
+ if (SystemConstants.DISABLE.equals(status) && this.countUserRoleByRoleId(roleId) > 0) {
throw new ServiceException("瑙掕壊宸插垎閰嶏紝涓嶈兘绂佺敤!");
}
return baseMapper.update(null,
@@ -324,6 +331,7 @@
* @param bo 瑙掕壊淇℃伅
* @return 缁撴灉
*/
+ @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, key = "#bo.roleId")
@Override
@Transactional(rollbackFor = Exception.class)
public int authDataScope(SysRoleBo bo) {
@@ -344,7 +352,7 @@
private int insertRoleMenu(SysRoleBo role) {
int rows = 1;
// 鏂板鐢ㄦ埛涓庤鑹茬鐞�
- List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
+ List<SysRoleMenu> list = new ArrayList<>();
for (Long menuId : role.getMenuIds()) {
SysRoleMenu rm = new SysRoleMenu();
rm.setRoleId(role.getRoleId());
@@ -365,7 +373,7 @@
private int insertRoleDept(SysRoleBo role) {
int rows = 1;
// 鏂板瑙掕壊涓庨儴闂紙鏁版嵁鏉冮檺锛夌鐞�
- List<SysRoleDept> list = new ArrayList<SysRoleDept>();
+ List<SysRoleDept> list = new ArrayList<>();
for (Long deptId : role.getDeptIds()) {
SysRoleDept rd = new SysRoleDept();
rd.setRoleId(role.getRoleId());
@@ -384,6 +392,7 @@
* @param roleId 瑙掕壊ID
* @return 缁撴灉
*/
+ @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, key = "#roleId")
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteRoleById(Long roleId) {
@@ -400,6 +409,7 @@
* @param roleIds 闇�瑕佸垹闄ょ殑瑙掕壊ID
* @return 缁撴灉
*/
+ @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, allEntries = true)
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteRoleByIds(Long[] roleIds) {
@@ -431,7 +441,7 @@
.eq(SysUserRole::getRoleId, userRole.getRoleId())
.eq(SysUserRole::getUserId, userRole.getUserId()));
if (rows > 0) {
- cleanOnlineUserByRole(userRole.getRoleId());
+ cleanOnlineUser(List.of(userRole.getUserId()));
}
return rows;
}
@@ -445,11 +455,12 @@
*/
@Override
public int deleteAuthUsers(Long roleId, Long[] userIds) {
+ List<Long> ids = List.of(userIds);
int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getRoleId, roleId)
- .in(SysUserRole::getUserId, Arrays.asList(userIds)));
+ .in(SysUserRole::getUserId, ids));
if (rows > 0) {
- cleanOnlineUserByRole(roleId);
+ cleanOnlineUser(ids);
}
return rows;
}
@@ -465,7 +476,8 @@
public int insertAuthUsers(Long roleId, Long[] userIds) {
// 鏂板鐢ㄦ埛涓庤鑹茬鐞�
int rows = 1;
- List<SysUserRole> list = StreamUtils.toList(List.of(userIds), userId -> {
+ List<Long> ids = List.of(userIds);
+ List<SysUserRole> list = StreamUtils.toList(ids, userId -> {
SysUserRole ur = new SysUserRole();
ur.setUserId(userId);
ur.setRoleId(roleId);
@@ -475,7 +487,7 @@
rows = userRoleMapper.insertBatch(list) ? list.size() : 0;
}
if (rows > 0) {
- cleanOnlineUserByRole(roleId);
+ cleanOnlineUser(ids);
}
return rows;
}
@@ -499,6 +511,9 @@
return;
}
LoginUser loginUser = LoginHelper.getLoginUser(token);
+ if (ObjectUtil.isNull(loginUser) || CollUtil.isEmpty(loginUser.getRoles())) {
+ return;
+ }
if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) {
try {
StpUtil.logoutByTokenValue(token);
@@ -507,4 +522,31 @@
}
});
}
+
+ @Override
+ public void cleanOnlineUser(List<Long> userIds) {
+ List<String> keys = StpUtil.searchTokenValue("", 0, -1, false);
+ if (CollUtil.isEmpty(keys)) {
+ return;
+ }
+ // 瑙掕壊鍏宠仈鐨勫湪绾跨敤鎴烽噺杩囧ぇ浼氬鑷磖edis闃诲鍗¢】 璋ㄦ厧鎿嶄綔
+ keys.parallelStream().forEach(key -> {
+ String token = StringUtils.substringAfterLast(key, ":");
+ // 濡傛灉宸茬粡杩囨湡鍒欒烦杩�
+ if (StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) < -1) {
+ return;
+ }
+ LoginUser loginUser = LoginHelper.getLoginUser(token);
+ if (ObjectUtil.isNull(loginUser)) {
+ return;
+ }
+ if (userIds.contains(loginUser.getUserId())) {
+ try {
+ StpUtil.logoutByTokenValue(token);
+ } catch (NotLoginException ignored) {
+ }
+ }
+ });
+ }
+
}
--
Gitblit v1.9.3