修改注销逻辑

parent 3e3e5d54
......@@ -60,6 +60,7 @@ public interface Constants {
interface Session {
String USER_SESSION_CACHE = "user:session:";
String USER_SESSION_ID_CACHE = "userid-sessionvalue:cache:";
String USER_SESSION_KEY_SET = "userid-keys:set:";
Long ONE_DAY = 24 * 60 * 60L;
}
......
......@@ -10,7 +10,9 @@ import cn.quantgroup.xyqb.model.session.SessionValue;
import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.session.aspect.UserBtRegisterFill;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import jdk.nashorn.internal.parser.JSONParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -20,10 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
......@@ -133,6 +132,26 @@ public class SessionServiceImpl implements ISessionService {
Constants.Session.ONE_DAY, TimeUnit.SECONDS);
String key = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties());
stringRedisTemplate.opsForValue().set(key, token, Constants.Session.ONE_DAY, TimeUnit.SECONDS);
setUserIdTokenKeys(sessionValue, key);
}
/**
* 设置用户token集合方便注销使用
* @param sessionValue
* @param key
*/
private void setUserIdTokenKeys(SessionValue sessionValue, String key) {
String useIdKeys = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_KEY_SET + sessionValue.getUser().getId());
Set useIdKeySet = null;
if (StringUtils.isNotEmpty(useIdKeys)) {
useIdKeySet = JSON.parseObject(useIdKeys, Set.class);
} else {
useIdKeySet = new HashSet();
}
useIdKeySet.add(key);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSION_KEY_SET + sessionValue.getUser().getId(), JSONObject.toJSONString(useIdKeySet), Constants.Session.ONE_DAY, TimeUnit.SECONDS);
}
@Override
......@@ -158,13 +177,21 @@ public class SessionServiceImpl implements ISessionService {
@Override
public void deleteByUserId(long userId) {
String pattern = Constants.Session.USER_SESSION_ID_CACHE + ":" + String.valueOf(userId) + ":*";
Set<String> keys = stringRedisTemplate.keys(pattern);
if (!CollectionUtils.isEmpty(keys)) {
log.info("删除用户userId={}的缓存信息,个数:{},keys={}", userId,
keys.size(),
Joiner.on(",").join(keys));
}
stringRedisTemplate.delete(keys);
// String pattern = Constants.Session.USER_SESSION_ID_CACHE + ":" + String.valueOf(userId) + ":*";
// Set<String> keys = stringRedisTemplate.keys(pattern);
// if (!CollectionUtils.isEmpty(keys)) {
// log.info("删除用户userId={}的缓存信息,个数:{},keys={}", userId,
// keys.size(),
// Joiner.on(",").join(keys));
// }
String useIdKeys = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_KEY_SET + userId);
if (StringUtils.isNotEmpty(useIdKeys)) {
Set useIdKeySet = JSON.parseObject(useIdKeys, Set.class);
useIdKeySet.forEach(key -> {
stringRedisTemplate.delete(String.valueOf(key));
});
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment