@@ -62,13 +62,26 @@ public List<OnlineUserResp> list(OnlineUserQuery query) {
62
62
List <OnlineUserResp > list = new ArrayList <>();
63
63
// 查询所有在线 Token
64
64
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
+ );
72
85
// 筛选数据
73
86
for (Map .Entry <Long , List <String >> entry : tokenMap .entrySet ()) {
74
87
Long userId = entry .getKey ();
0 commit comments