feat: 解耦认证逻辑

This commit is contained in:
2026-06-24 12:17:09 +08:00
parent 5d9f5b8ab7
commit ab2a2dbf7f
14 changed files with 332 additions and 94 deletions
@@ -1,6 +1,6 @@
package com.webgame.webgamebackend.ws;
import cn.dev33.satoken.stp.StpUtil;
import com.webgame.webgamebackend.common.auth.AuthenticationProvider;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.webgame.webgamebackend.service.GomokuGameService;
@@ -35,6 +35,7 @@ public class RoomWebSocketHandler extends TextWebSocketHandler {
private final RoomSessionManager sessionManager;
private final GomokuGameService gomokuGameService;
private final WsMessageRouter router;
private final AuthenticationProvider authProvider;
/**
* 注册所有消息类型与对应处理器
@@ -109,12 +110,11 @@ public class RoomWebSocketHandler extends TextWebSocketHandler {
}
// 通过 token 获取用户 ID
Object loginId = StpUtil.getLoginIdByToken(token);
if (loginId == null) {
String userId = authProvider.getUserIdByToken(token);
if (userId == null) {
session.close(CloseStatus.POLICY_VIOLATION);
return;
}
String userId = loginId.toString();
// 注册会话(内部会清理同用户的旧会话)
sessionManager.addSession(roomId, userId, session);