优化用户删除的接口,删除时将被删除的用户的登录状态从redis移除

This commit is contained in:
jishenghua
2025-11-10 23:18:36 +08:00
parent b7613d4684
commit c56d7d2e81
2 changed files with 32 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package com.jsh.erp.service;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
@@ -153,6 +154,8 @@ public class RedisService {
public void deleteObjectByUserAndIp(Long userId, String clientIp){
Set<String> tokens = redisTemplate.keys("*");
for(String token : tokens) {
// 检查键是否存在且为哈希类型
if (redisTemplate.hasKey(token) && redisTemplate.type(token) == DataType.HASH) {
Object userIdValue = redisTemplate.opsForHash().get(token, "userId");
Object clientIpValue = redisTemplate.opsForHash().get(token, "clientIp");
if(userIdValue!=null && clientIpValue!=null && userIdValue.equals(userId.toString()) && clientIpValue.equals(clientIp)) {
@@ -160,4 +163,23 @@ public class RedisService {
}
}
}
}
/**
* @author jisheng hua
* 将信息从redis中移除比对user
* @param userId
*/
public void deleteObjectByUser(Long userId){
Set<String> tokens = redisTemplate.keys("*");
for(String token : tokens) {
// 检查键是否存在且为哈希类型
if (redisTemplate.hasKey(token) && redisTemplate.type(token) == DataType.HASH) {
Object userIdValue = redisTemplate.opsForHash().get(token, "userId");
if(userIdValue!=null && userIdValue.equals(userId.toString())) {
redisTemplate.opsForHash().delete(token, "userId");
}
}
}
}
}

View File

@@ -267,6 +267,12 @@ public class UserService {
Object userId = redisService.getObjectFromSessionByKey(request,"userId");
if (userId != null) {
result = userMapperEx.batDeleteOrUpdateUser(idsArray);
if(result>0) {
//从redis中移除这些用户的登录状态
for (String idStr : idsArray) {
redisService.deleteObjectByUser(Long.valueOf(idStr));
}
}
logService.insertLog("用户", sb.toString(),
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
}