liujie
2026-07-10 e7742650566220c30491262d34d69ffa4effb366
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.ruoyi.web.controller.webSocket;
 
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUserApplet;
import com.ruoyi.common.core.redis.RedisCache;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.util.Map;
 
/**
 * WebSocket认证处理器
 * 处理WebSocket连接的用户认证逻辑
 */
@Component
@Slf4j
public class WebSocketAuthHandler {
 
 
    @Autowired
    private RedisCache redisCache;
    
    // 令牌秘钥
    @Value("${token.secret}")
    private String secret;
 
 
 
    /**
     * 验证token
     * @param appUserId 用户ID
     * @param token 令牌
     * @return 是否有效
     */
    private boolean validateToken(String appUserId, String token) {
        try {
            if (!StringUtils.hasText(token)) {
                log.debug("用户 {} 未提供token,跳过token验证", appUserId);
                return true; // 如果没有提供token,允许通过(可以根据业务需求调整)
            }
            
            // 移除Bearer前缀
            if (token.startsWith(Constants.TOKEN_PREFIX)) {
                token = token.replace(Constants.TOKEN_PREFIX, "");
            }
            
            // 解析JWT token
            Claims claims = parseToken(token);
            if (claims == null) {
                log.warn("用户 {} 的token解析失败", appUserId);
                return false;
            }
            
            // 从token中获取用户标识
            String uuid = (String) claims.get(Constants.LOGIN_USER_APPLET_KEY);
            if (!StringUtils.hasText(uuid)) {
                log.warn("用户 {} 的token中未找到用户标识", appUserId);
                return false;
            }
            
            // 从Redis中获取用户登录信息
            String userKey = getTokenKey(uuid);
            LoginUserApplet loginUser = redisCache.getCacheObject(userKey);
            
            if (loginUser == null) {
                log.warn("用户 {} 的登录信息已过期或不存在", appUserId);
                return false;
            }
            
            // 验证用户ID是否匹配
            if (!appUserId.equals(loginUser.getUserId())) {
                log.warn("用户 {} 的token与提供的用户ID不匹配", appUserId);
                return false;
            }
            
            // 验证token是否过期
            long currentTime = System.currentTimeMillis();
            if (loginUser.getExpireTime() != null && loginUser.getExpireTime() < currentTime) {
                log.warn("用户 {} 的token已过期", appUserId);
                return false;
            }
            
            log.debug("用户 {} 的token验证成功", appUserId);
            return true;
            
        } catch (Exception e) {
            log.error("验证用户 {} 的token时发生错误: {}", appUserId, e.getMessage(), e);
            return false;
        }
    }
    
    /**
     * 解析JWT token
     * @param token JWT token
     * @return Claims对象
     */
    private Claims parseToken(String token) {
        try {
            return Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            log.error("解析token失败: {}", e.getMessage());
            return null;
        }
    }
    
    /**
     * 获取token在Redis中的key
     * @param uuid 用户唯一标识
     * @return Redis key
     */
    private String getTokenKey(String uuid) {
        return com.ruoyi.common.constant.CacheConstants.LOGIN_TOKEN_KEY + uuid;
    }
 
 
 
    /**
     * 从认证消息中提取用户信息
     * @param messageObj 消息对象
     * @return 用户认证信息
     */
    public UserAuthInfo extractAuthInfo(Map<String, Object> messageObj) {
        String appUserId = (String) messageObj.get("appUserId");
        String token = (String) messageObj.get("token");
        String deviceId = (String) messageObj.get("deviceId");
        String deviceType = (String) messageObj.get("deviceType");
 
        return new UserAuthInfo(appUserId, token, deviceId, deviceType);
    }
 
    /**
     * 用户认证信息类
     */
    public static class UserAuthInfo {
        private final String appUserId;
        private final String token;
        private final String deviceId;
        private final String deviceType;
 
        public UserAuthInfo(String appUserId, String token, String deviceId, String deviceType) {
            this.appUserId = appUserId;
            this.token = token;
            this.deviceId = deviceId;
            this.deviceType = deviceType;
        }
 
        public String getAppUserId() {
            return appUserId;
        }
 
        public String getToken() {
            return token;
        }
 
        public String getDeviceId() {
            return deviceId;
        }
 
        public String getDeviceType() {
            return deviceType;
        }
    }
}