优化用户删除的接口,删除时将被删除的用户的登录状态从redis移除
This commit is contained in:
@@ -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)) {
|
||||
@@ -161,3 +164,22 @@ 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user