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;
@@ -19,7 +20,7 @@
 * 1. 前端连接 websocket 时,会通过拼接 ?token={token} 到 ws:// 连接后,这样它可以被 {@link TokenAuthenticationFilter} 所认证通过
 * 2. {@link LoginUserHandshakeInterceptor} 负责把 {@link LoginUser} 添加到 {@link WebSocketSession} 中
 *
 * @author 芋道源码
 * @author 超级管理员
 */
public class LoginUserHandshakeInterceptor implements HandshakeInterceptor {
@@ -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;
    }