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

This commit is contained in:
Billy
2025-07-02 15:56:37 +08:00
parent d4df4254fc
commit 53fc674f4a

View File

@@ -62,13 +62,26 @@ public class OnlineUserServiceImpl implements OnlineUserService {
List<OnlineUserResp> list = new ArrayList<>();
// 查询所有在线 Token
List<String> tokenKeyList = StpUtil.searchTokenValue(StringConstants.EMPTY, 0, -1, false);
Map<Long, List<String>> tokenMap = tokenKeyList.stream().filter(tokenKey -> {
String token = StrUtil.subAfter(tokenKey, StringConstants.COLON, true);
// 忽略已过期或失效 Token
return StpUtil.getStpLogic().getTokenActiveTimeoutByToken(token) >= SaTokenDao.NEVER_EXPIRE;
})
.map(tokenKey -> StrUtil.subAfter(tokenKey, StringConstants.COLON, true))
.collect(Collectors.groupingBy(token -> Convert.toLong(StpUtil.getLoginIdByToken(token))));
Map<Long, List<String>> tokenMap = tokenKeyList.stream()
// 提前映射,避免重复调用
.map(tokenKey -> StrUtil.subAfter(tokenKey, StringConstants.COLON, true))
.map(token -> {
Object loginIdObj = StpUtil.getLoginIdByToken(token);
long tokenTimeout = StpUtil.getStpLogic().getTokenActiveTimeoutByToken(token);
// 将相关信息打包成对象或简单的Entry对便于后续过滤与归类
return new AbstractMap.SimpleEntry<>(token, new AbstractMap.SimpleEntry<>(loginIdObj, tokenTimeout));
})
// 过滤出未过期且loginId存在的Token
.filter(entry -> {
Object loginIdObj = entry.getValue().getKey();
long tokenTimeout = entry.getValue().getValue();
return loginIdObj != null && tokenTimeout >= SaTokenDao.NEVER_EXPIRE;
})
// 此时数据都有效,进行收集
.collect(Collectors.groupingBy(
entry -> Convert.toLong(entry.getValue().getKey()),
Collectors.mapping(AbstractMap.SimpleEntry::getKey, Collectors.toList()))
);
// 筛选数据
for (Map.Entry<Long, List<String>> entry : tokenMap.entrySet()) {
Long userId = entry.getKey();