重构项目结构:迁移到单体架构并优化代码组织
- 删除分布式架构相关文件和配置 - 将backend-distributed重命名为backend保留分布式代码作为参考 - 优化backend-single单体架构实现 - 添加Coze API集成相关文档和测试 - 清理项目根目录的部署脚本和配置文件 - 更新WebSocket和消息服务实现 - 完善认证服务和密码加密功能
This commit is contained in:
+35
-24
@@ -35,12 +35,13 @@ public class WebSocketAuthInterceptor implements ChannelInterceptor {
|
||||
@Override
|
||||
public Message<?> preSend(Message<?> message, MessageChannel channel) {
|
||||
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
|
||||
|
||||
|
||||
if (accessor != null && StompCommand.CONNECT.equals(accessor.getCommand())) {
|
||||
log.info("WebSocket CONNECT命令检测到,开始处理认证");
|
||||
// 处理WebSocket连接时的认证
|
||||
handleAuthentication(accessor);
|
||||
}
|
||||
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -52,46 +53,54 @@ public class WebSocketAuthInterceptor implements ChannelInterceptor {
|
||||
// 从连接头中获取token
|
||||
String authHeader = accessor.getFirstNativeHeader("Authorization");
|
||||
String userId = accessor.getFirstNativeHeader("X-User-Id");
|
||||
|
||||
log.info("WebSocket连接认证: authHeader={}, userId={}",
|
||||
authHeader != null ? "Bearer ***" : null, userId);
|
||||
|
||||
|
||||
log.info("WebSocket连接认证开始: authHeader={}, userId={}, sessionId={}",
|
||||
authHeader != null ? "Bearer ***" : null, userId, accessor.getSessionId());
|
||||
|
||||
if (StringUtils.hasText(authHeader) && authHeader.startsWith("Bearer ")) {
|
||||
String token = authHeader.substring(7);
|
||||
|
||||
log.info("提取到token: {}...", token.length() > 10 ? token.substring(0, 10) : token);
|
||||
|
||||
// 验证token
|
||||
if (authService.validateToken(token)) {
|
||||
boolean isValidToken = authService.validateToken(token);
|
||||
log.info("Token验证结果: {}", isValidToken);
|
||||
|
||||
if (isValidToken) {
|
||||
String tokenUserId = authService.getUserIdFromToken(token);
|
||||
String username = authService.getUsernameFromToken(token);
|
||||
|
||||
|
||||
log.info("WebSocket token验证成功: userId={}, username={}", tokenUserId, username);
|
||||
|
||||
|
||||
// 创建认证对象
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken(
|
||||
tokenUserId,
|
||||
null,
|
||||
tokenUserId,
|
||||
null,
|
||||
Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))
|
||||
);
|
||||
|
||||
|
||||
// 设置用户认证信息
|
||||
accessor.setUser(authentication);
|
||||
|
||||
|
||||
// 设置会话属性
|
||||
accessor.getSessionAttributes().put("userId", tokenUserId);
|
||||
accessor.getSessionAttributes().put("username", username);
|
||||
accessor.getSessionAttributes().put("authenticated", true);
|
||||
|
||||
if (accessor.getSessionAttributes() != null) {
|
||||
accessor.getSessionAttributes().put("userId", tokenUserId);
|
||||
accessor.getSessionAttributes().put("username", username);
|
||||
accessor.getSessionAttributes().put("authenticated", true);
|
||||
}
|
||||
|
||||
log.info("WebSocket认证用户设置完成: principal={}", authentication.getName());
|
||||
|
||||
} else {
|
||||
log.warn("WebSocket token验证失败: token无效");
|
||||
log.warn("WebSocket token验证失败: token无效或已过期");
|
||||
// token无效,但不阻止连接,作为访客处理
|
||||
handleGuestUser(accessor, userId);
|
||||
}
|
||||
} else {
|
||||
log.info("WebSocket连接无token,作为访客处理: userId={}", userId);
|
||||
log.info("WebSocket连接无token或格式错误,作为访客处理: userId={}", userId);
|
||||
// 无token,作为访客处理
|
||||
handleGuestUser(accessor, userId);
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("WebSocket认证处理失败", e);
|
||||
// 认证失败,作为访客处理
|
||||
@@ -117,8 +126,10 @@ public class WebSocketAuthInterceptor implements ChannelInterceptor {
|
||||
accessor.setUser(guestAuth);
|
||||
|
||||
// 设置会话属性
|
||||
accessor.getSessionAttributes().put("userId", guestId);
|
||||
accessor.getSessionAttributes().put("username", guestId);
|
||||
accessor.getSessionAttributes().put("authenticated", false);
|
||||
if (accessor.getSessionAttributes() != null) {
|
||||
accessor.getSessionAttributes().put("userId", guestId);
|
||||
accessor.getSessionAttributes().put("username", guestId);
|
||||
accessor.getSessionAttributes().put("authenticated", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user