优化用户删除的接口,删除时将被删除的用户的登录状态从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.constants.BusinessConstants;
|
||||||
import com.jsh.erp.utils.StringUtil;
|
import com.jsh.erp.utils.StringUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.RedisTemplate;
|
||||||
import org.springframework.data.redis.core.ValueOperations;
|
import org.springframework.data.redis.core.ValueOperations;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
@@ -153,10 +154,31 @@ public class RedisService {
|
|||||||
public void deleteObjectByUserAndIp(Long userId, String clientIp){
|
public void deleteObjectByUserAndIp(Long userId, String clientIp){
|
||||||
Set<String> tokens = redisTemplate.keys("*");
|
Set<String> tokens = redisTemplate.keys("*");
|
||||||
for(String token : tokens) {
|
for(String token : tokens) {
|
||||||
Object userIdValue = redisTemplate.opsForHash().get(token, "userId");
|
// 检查键是否存在且为哈希类型
|
||||||
Object clientIpValue = redisTemplate.opsForHash().get(token, "clientIp");
|
if (redisTemplate.hasKey(token) && redisTemplate.type(token) == DataType.HASH) {
|
||||||
if(userIdValue!=null && clientIpValue!=null && userIdValue.equals(userId.toString()) && clientIpValue.equals(clientIp)) {
|
Object userIdValue = redisTemplate.opsForHash().get(token, "userId");
|
||||||
redisTemplate.opsForHash().delete(token, "userId");
|
Object clientIpValue = redisTemplate.opsForHash().get(token, "clientIp");
|
||||||
|
if(userIdValue!=null && clientIpValue!=null && userIdValue.equals(userId.toString()) && clientIpValue.equals(clientIp)) {
|
||||||
|
redisTemplate.opsForHash().delete(token, "userId");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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");
|
Object userId = redisService.getObjectFromSessionByKey(request,"userId");
|
||||||
if (userId != null) {
|
if (userId != null) {
|
||||||
result = userMapperEx.batDeleteOrUpdateUser(idsArray);
|
result = userMapperEx.batDeleteOrUpdateUser(idsArray);
|
||||||
|
if(result>0) {
|
||||||
|
//从redis中移除这些用户的登录状态
|
||||||
|
for (String idStr : idsArray) {
|
||||||
|
redisService.deleteObjectByUser(Long.valueOf(idStr));
|
||||||
|
}
|
||||||
|
}
|
||||||
logService.insertLog("用户", sb.toString(),
|
logService.insertLog("用户", sb.toString(),
|
||||||
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
|
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user