Skip to content

Commit 53fc674

Browse files
committed
fix(monitor/online): 修复了在过滤无效 token 时,没有增加对StpUtil.getLoginIdByToken 返回 null情况处理,导致 执行 groupingBy 报错
1 parent d4df425 commit 53fc674

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

continew-system/src/main/java/top/continew/admin/auth/service/impl/OnlineUserServiceImpl.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,26 @@ public List<OnlineUserResp> list(OnlineUserQuery query) {
6262
List<OnlineUserResp> list = new ArrayList<>();
6363
// 查询所有在线 Token
6464
List<String> tokenKeyList = StpUtil.searchTokenValue(StringConstants.EMPTY, 0, -1, false);
65-
Map<Long, List<String>> tokenMap = tokenKeyList.stream().filter(tokenKey -> {
66-
String token = StrUtil.subAfter(tokenKey, StringConstants.COLON, true);
67-
// 忽略已过期或失效 Token
68-
return StpUtil.getStpLogic().getTokenActiveTimeoutByToken(token) >= SaTokenDao.NEVER_EXPIRE;
69-
})
70-
.map(tokenKey -> StrUtil.subAfter(tokenKey, StringConstants.COLON, true))
71-
.collect(Collectors.groupingBy(token -> Convert.toLong(StpUtil.getLoginIdByToken(token))));
65+
Map<Long, List<String>> tokenMap = tokenKeyList.stream()
66+
// 提前映射,避免重复调用
67+
.map(tokenKey -> StrUtil.subAfter(tokenKey, StringConstants.COLON, true))
68+
.map(token -> {
69+
Object loginIdObj = StpUtil.getLoginIdByToken(token);
70+
long tokenTimeout = StpUtil.getStpLogic().getTokenActiveTimeoutByToken(token);
71+
// 将相关信息打包成对象或简单的Entry对,便于后续过滤与归类
72+
return new AbstractMap.SimpleEntry<>(token, new AbstractMap.SimpleEntry<>(loginIdObj, tokenTimeout));
73+
})
74+
// 过滤出未过期且loginId存在的Token
75+
.filter(entry -> {
76+
Object loginIdObj = entry.getValue().getKey();
77+
long tokenTimeout = entry.getValue().getValue();
78+
return loginIdObj != null && tokenTimeout >= SaTokenDao.NEVER_EXPIRE;
79+
})
80+
// 此时数据都有效,进行收集
81+
.collect(Collectors.groupingBy(
82+
entry -> Convert.toLong(entry.getValue().getKey()),
83+
Collectors.mapping(AbstractMap.SimpleEntry::getKey, Collectors.toList()))
84+
);
7285
// 筛选数据
7386
for (Map.Entry<Long, List<String>> entry : tokenMap.entrySet()) {
7487
Long userId = entry.getKey();

0 commit comments

Comments
 (0)