1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
@Service @RequiredArgsConstructor public class TerminalAndTokenControlService {
private final TokenService tokenService; private final CacheService cacheService; private final JwtService jwtService;
private final AuthTokenProperties authTokenProperties;
public String doControl(String userId, String subValue, String termValue) { AuthTokenProperties.Subject subject = authTokenProperties.getMapSubject().get(subValue); if (subject == null) { throw new BaseException(ErrorCodeEnum.FAIL, "subject[" + subValue + "] 未配置"); } AuthTokenProperties.Terminal terminal = subject.getMapTerminal().get(termValue); if (terminal == null) { throw new BaseException(ErrorCodeEnum.FAIL, "terminal[" + termValue + "] 不正确"); }
String token = tokenService.generateToken(userId, subValue, termValue); JwtPayloadVo jwtPayloadVo = jwtService.decodePayload(token); long timestampMs = jwtPayloadVo.getIatMs();
Map<String, String> mapTerminalInfo = tokenService.getSubjectTerminalInfo(subValue, userId); Map<String, String> mapTerminalInfoValid = new HashMap<>(); Map<String, String> mapTerminalInfoExpired = new HashMap<>(); mapTerminalInfo.forEach((term, termInfo) -> { long expTime = Long.parseLong(termInfo.split(",")[1]); if (timestampMs >= expTime) { mapTerminalInfoExpired.put(term, termInfo); } else { mapTerminalInfoValid.put(term, termInfo); } }); Map<String, Map<String, String>> mapTotalTerminalTokenInfo = new HashMap<>(); Map<String, Map<String, String>> mapRemainTerminalTokenInfo = new HashMap<>(); Map<String, String> mapTotalTerminalTokenInfoValid = new HashMap<>(); Map<String, String> mapTotalTerminalTokenInfoExpired = new HashMap<>(); for (Map.Entry<String, String> entry : mapTerminalInfo.entrySet()) { Map<String, String> mapTerminalTokenInfo = tokenService.getTerminalTokenInfo(subValue, userId, entry.getKey()); mapTotalTerminalTokenInfo.put(entry.getKey(), mapTerminalTokenInfo); } Map<String, String> mapTerminalTokenInfo = mapTotalTerminalTokenInfo.getOrDefault(termValue, new HashMap<>()); Map<String, String> mapTerminalTokenInfoValid = new HashMap<>(); Map<String, String> mapTerminalTokenInfoExpired = new HashMap<>(); mapTerminalTokenInfo.forEach((timestamp, expTime) -> { long expTimeInt = Long.parseLong(expTime); if (timestampMs >= expTimeInt) { mapTerminalTokenInfoExpired.put(timestamp, expTime); } else { mapTerminalTokenInfoValid.put(timestamp, expTime); } });
boolean clearAllTerminal = false; Set<String> needDeleteTerminal = new HashSet<>(); Map<String, Set<String>> mapNeedDeleteTokenInfo = new HashMap<>();
if (subject.getMaxOnlineTerminalCount() > 0) { int occurTerminalCount = mapTerminalInfoValid.size() + (mapTerminalInfoValid.containsKey(termValue) ? 0 : 1); if (occurTerminalCount > subject.getMaxOnlineTerminalCount()) { int needToDeleteTerminalCount = occurTerminalCount - subject.getMaxOnlineTerminalCount(); Set<String> eliminatedTerminal = mapTerminalInfoValid.entrySet() .stream().sorted(Map.Entry.comparingByValue()).limit(needToDeleteTerminalCount) .map(Map.Entry::getKey).collect(Collectors.toSet()); needDeleteTerminal.addAll(eliminatedTerminal); } } else if (subject.getMaxOnlineTerminalCount() == 0) { clearAllTerminal = true; } needDeleteTerminal.addAll(mapTerminalInfoExpired.keySet());
if (subject.getMaxOnlineTokenCount() > 0) { int occurTokenCount = 0; mapRemainTerminalTokenInfo = mapTotalTerminalTokenInfo.entrySet().stream() .filter(entry -> !needDeleteTerminal.contains(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); for (Map.Entry<String, Map<String, String>> entry : mapRemainTerminalTokenInfo.entrySet()) { occurTokenCount += entry.getValue().size(); entry.getValue().forEach((timestamp, expTime) -> { long expTimeInt = Long.parseLong(expTime); if (timestampMs >= expTimeInt) { mapTotalTerminalTokenInfoExpired.put(timestamp + "," + entry.getKey(), expTime); } else { mapTotalTerminalTokenInfoValid.put(timestamp + "," + entry.getKey(), expTime); } }); } if (occurTokenCount >= subject.getMaxOnlineTokenCount()) { int needDeleteCount = occurTokenCount - subject.getMaxOnlineTokenCount() + 1; Set<String> eliminatedTokenInfo = mapTotalTerminalTokenInfoValid.entrySet() .stream().sorted((e1, e2) -> { String terminal1 = e1.getKey().split(",")[1]; int sort = Objects.equals(terminal1, termValue) ? 0 : 1; String key1 = sort + ":" + e1.getKey();
String terminal2 = e2.getKey().split(",")[1]; sort = Objects.equals(terminal2, termValue) ? 0 : 1; String key2 = sort + ":" + e2.getKey();
return key1.compareTo(key2); }).limit(needDeleteCount) .map(Map.Entry::getKey).collect(Collectors.toSet()); eliminatedTokenInfo.forEach(e -> { String[] split = e.split(","); mapNeedDeleteTokenInfo.computeIfAbsent(split[1], k -> new HashSet<>()).add(split[0]); }); } } else if (subject.getMaxOnlineTokenCount() == 0) { clearAllTerminal = true; } mapTotalTerminalTokenInfoExpired.keySet().forEach(e -> { String[] split = e.split(","); mapNeedDeleteTokenInfo.computeIfAbsent(split[1], k -> new HashSet<>()).add(split[0]); });
if (!needDeleteTerminal.contains(termValue)) { if (terminal.getMaxOnlineTokenCount() > 0) { if (mapTerminalTokenInfoValid.size() >= terminal.getMaxOnlineTokenCount()) { int needDeleteCount = mapTerminalTokenInfoValid.size() - terminal.getMaxOnlineTokenCount() + 1; Set<String> eliminatedToken = mapTerminalTokenInfoValid.entrySet() .stream().sorted(Map.Entry.comparingByKey()).limit(needDeleteCount) .map(Map.Entry::getKey).collect(Collectors.toSet()); eliminatedToken.forEach(timestamp -> { mapNeedDeleteTokenInfo.computeIfAbsent(termValue, k -> new HashSet<>()).add(timestamp); }); } } else if (terminal.getMaxOnlineTokenCount() == 0) { needDeleteTerminal.add(termValue); } mapTerminalTokenInfoExpired.keySet().forEach(timestamp -> { mapNeedDeleteTokenInfo.computeIfAbsent(termValue, k -> new HashSet<>()).add(timestamp); }); }
if (clearAllTerminal) { Set<String> delKeys = new HashSet<>(); delKeys.add(String.format(CacheKeyConsts.USER_SUBJECT_TERMINAL_INFO, subValue, userId)); mapTerminalInfo.keySet().forEach(term -> { delKeys.add(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN_INFO, subValue, userId, term)); mapTotalTerminalTokenInfo.getOrDefault(term, new HashMap<>()).forEach((timestamp, expTime) -> { delKeys.add(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN, subValue, userId, term, timestamp)); }); }); cacheService.delete(delKeys.toArray(new String[0])); throw new BaseException("禁止登录"); } else { Set<String> delKeys = new HashSet<>(); Map<String, Set<String>> delFields = new HashMap<>();
needDeleteTerminal.forEach(term -> { delFields.computeIfAbsent(String.format(CacheKeyConsts.USER_SUBJECT_TERMINAL_INFO, subValue, userId), k -> new HashSet<>()).add(term);
delKeys.add(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN_INFO, subValue, userId, term)); Optional.ofNullable(mapTotalTerminalTokenInfo.get(term)).ifPresent(terminalTokenInfo -> { terminalTokenInfo.keySet().forEach(timestamp -> { delFields.computeIfAbsent(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN_INFO, subValue, userId, term), k -> new HashSet<>()).add(timestamp); delKeys.add(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN, subValue, userId, term, timestamp)); }); }); }); mapNeedDeleteTokenInfo.forEach((term, timestampSet) -> { delFields.computeIfAbsent(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN_INFO, subValue, userId, term), k -> new HashSet<>()).addAll(timestampSet); timestampSet.forEach(timestamp -> { delKeys.add(String.format(CacheKeyConsts.USER_TERMINAL_TOKEN, subValue, userId, term, timestamp)); }); }); cacheService.delete(delKeys.toArray(new String[0])); delFields.forEach((cacheKey, fields) -> { cacheService.deleteHashFields(cacheKey, fields.toArray(new Object[0])); });
tokenService.setSubjectTerminalInfo(subValue, userId, termValue, jwtPayloadVo.getIatMs(), jwtPayloadVo.getExpMs()); tokenService.setTerminalTokenInfo(subValue, userId, termValue, jwtPayloadVo.getIatMs(), jwtPayloadVo.getExpMs()); tokenService.cacheToken(subValue, userId, termValue, token, timestampMs); return token; } } }
|