修改注销逻辑

parent 3e3e5d54
...@@ -60,6 +60,7 @@ public interface Constants { ...@@ -60,6 +60,7 @@ public interface Constants {
interface Session { interface Session {
String USER_SESSION_CACHE = "user:session:"; String USER_SESSION_CACHE = "user:session:";
String USER_SESSION_ID_CACHE = "userid-sessionvalue:cache:"; String USER_SESSION_ID_CACHE = "userid-sessionvalue:cache:";
String USER_SESSION_KEY_SET = "userid-keys:set:";
Long ONE_DAY = 24 * 60 * 60L; Long ONE_DAY = 24 * 60 * 60L;
} }
......
...@@ -10,7 +10,9 @@ import cn.quantgroup.xyqb.model.session.SessionValue; ...@@ -10,7 +10,9 @@ import cn.quantgroup.xyqb.model.session.SessionValue;
import cn.quantgroup.xyqb.service.session.ISessionService; import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.session.aspect.UserBtRegisterFill; import cn.quantgroup.xyqb.service.session.aspect.UserBtRegisterFill;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import jdk.nashorn.internal.parser.JSONParser;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -20,10 +22,7 @@ import org.springframework.stereotype.Service; ...@@ -20,10 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
...@@ -133,6 +132,26 @@ public class SessionServiceImpl implements ISessionService { ...@@ -133,6 +132,26 @@ public class SessionServiceImpl implements ISessionService {
Constants.Session.ONE_DAY, TimeUnit.SECONDS); Constants.Session.ONE_DAY, TimeUnit.SECONDS);
String key = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties()); String key = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties());
stringRedisTemplate.opsForValue().set(key, token, Constants.Session.ONE_DAY, TimeUnit.SECONDS); 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 @Override
...@@ -158,13 +177,21 @@ public class SessionServiceImpl implements ISessionService { ...@@ -158,13 +177,21 @@ public class SessionServiceImpl implements ISessionService {
@Override @Override
public void deleteByUserId(long userId) { public void deleteByUserId(long userId) {
String pattern = Constants.Session.USER_SESSION_ID_CACHE + ":" + String.valueOf(userId) + ":*"; // String pattern = Constants.Session.USER_SESSION_ID_CACHE + ":" + String.valueOf(userId) + ":*";
Set<String> keys = stringRedisTemplate.keys(pattern); // Set<String> keys = stringRedisTemplate.keys(pattern);
if (!CollectionUtils.isEmpty(keys)) { // if (!CollectionUtils.isEmpty(keys)) {
log.info("删除用户userId={}的缓存信息,个数:{},keys={}", userId, // log.info("删除用户userId={}的缓存信息,个数:{},keys={}", userId,
keys.size(), // keys.size(),
Joiner.on(",").join(keys)); // Joiner.on(",").join(keys));
} // }
stringRedisTemplate.delete(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