update 优化 角色清理在线用户代码逻辑

This commit is contained in:
疯狂的狮子Li 2024-10-28 10:21:45 +08:00
parent ddc8bd1139
commit 6731b7947b
2 changed files with 38 additions and 5 deletions

View File

@ -197,4 +197,6 @@ public interface ISysRoleService {
void cleanOnlineUserByRole(Long roleId); void cleanOnlineUserByRole(Long roleId);
void cleanOnlineUser(List<Long> userIds);
} }

View File

@ -435,7 +435,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
.eq(SysUserRole::getRoleId, userRole.getRoleId()) .eq(SysUserRole::getRoleId, userRole.getRoleId())
.eq(SysUserRole::getUserId, userRole.getUserId())); .eq(SysUserRole::getUserId, userRole.getUserId()));
if (rows > 0) { if (rows > 0) {
cleanOnlineUserByRole(userRole.getRoleId()); cleanOnlineUser(List.of(userRole.getUserId()));
} }
return rows; return rows;
} }
@ -449,11 +449,12 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/ */
@Override @Override
public int deleteAuthUsers(Long roleId, Long[] userIds) { public int deleteAuthUsers(Long roleId, Long[] userIds) {
List<Long> ids = List.of(userIds);
int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getRoleId, roleId) .eq(SysUserRole::getRoleId, roleId)
.in(SysUserRole::getUserId, Arrays.asList(userIds))); .in(SysUserRole::getUserId, ids));
if (rows > 0) { if (rows > 0) {
cleanOnlineUserByRole(roleId); cleanOnlineUser(ids);
} }
return rows; return rows;
} }
@ -469,7 +470,8 @@ public class SysRoleServiceImpl implements ISysRoleService {
public int insertAuthUsers(Long roleId, Long[] userIds) { public int insertAuthUsers(Long roleId, Long[] userIds) {
// 新增用户与角色管理 // 新增用户与角色管理
int rows = 1; 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(); SysUserRole ur = new SysUserRole();
ur.setUserId(userId); ur.setUserId(userId);
ur.setRoleId(roleId); ur.setRoleId(roleId);
@ -479,7 +481,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
rows = userRoleMapper.insertBatch(list) ? list.size() : 0; rows = userRoleMapper.insertBatch(list) ? list.size() : 0;
} }
if (rows > 0) { if (rows > 0) {
cleanOnlineUserByRole(roleId); cleanOnlineUser(ids);
} }
return rows; return rows;
} }
@ -503,6 +505,9 @@ public class SysRoleServiceImpl implements ISysRoleService {
return; return;
} }
LoginUser loginUser = LoginHelper.getLoginUser(token); LoginUser loginUser = LoginHelper.getLoginUser(token);
if (ObjectUtil.isNull(loginUser) || CollUtil.isEmpty(loginUser.getRoles())) {
return;
}
if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) { if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) {
try { try {
StpUtil.logoutByTokenValue(token); StpUtil.logoutByTokenValue(token);
@ -511,4 +516,30 @@ public class SysRoleServiceImpl implements ISysRoleService {
} }
}); });
} }
@Override
public void cleanOnlineUser(List<Long> userIds) {
List<String> keys = StpUtil.searchTokenValue("", 0, -1, false);
if (CollUtil.isEmpty(keys)) {
return;
}
// 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作
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) {
}
}
});
}
} }