gaoluyang
6 天以前 c55aca77e84607238c3354168a8642176c575ca1
yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/security/LoginUserHandshakeInterceptor.java
@@ -1,5 +1,6 @@
package cn.iocoder.yudao.framework.websocket.core.security;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.filter.TokenAuthenticationFilter;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
@@ -30,6 +31,12 @@
        if (loginUser == null) {
            return false;
        }
        // WebSocket 地址(如 /infra/ws)不带 /admin-api、/app-api 前缀,
        // 因此 mock 登录时 WebFrameworkUtils#getLoginUserType 取不到用户类型。
        // 这里兜底为管理员类型,避免 WebSocketSessionManagerImpl 用 null 作为 key 记录会话时抛 NPE 断开连接。
        if (loginUser.getUserType() == null) {
            loginUser.setUserType(UserTypeEnum.ADMIN.getValue());
        }
        WebSocketFrameworkUtils.setLoginUser(loginUser, attributes);
        return true;
    }