Spring Boot 4.3.5 使用 JWT 授权的 WebSocket 聊天。GenericMessage 中没有目标。

11

我正在尝试为一个移动应用程序(ionic 3)实现1对1聊天,使用Spring Boot后端,看起来遇到了一些配置问题。

无法发送消息,可能是因为目标通道没有被创建。

后端:

聊天控制器(ChatController):

@RestController
public class ChatController {
    @Autowired
    private PrivateChatService privateChatService;

    private final static Logger logger = LogManager.getLogger(ChatController.class.getName());


    @RequestMapping(value = "/chat/messages/{item_id}/chat_with/{buyer_login}", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<String> getExistingChatMessages(@PathVariable("item_id") String itemId, @PathVariable("buyer_login") String buyerLogin) {
        List<ChatMessage> messages = privateChatService.getExistingChatMessages(itemId, buyerLogin);

        logger.info("Here get messages");
        return JSONResponseHelper.createResponse(messages, HttpStatus.OK);
    }


    @MessageMapping("/chat/{item_id}/send")
    @SendTo("/topic/chat/{item_id}/chat_with/{buyer_login}")
    public ChatMessage send(@Payload ChatMessage message,
                            @DestinationVariable("item_id") String item_id) throws Exception {
//        logger.info(principal.getName());
        logger.info(message.toString());
        logger.info(item_id);
        privateChatService.submitMessage(message);
        return message;
    }

}

WebSocketConfig:

@Configuration
@EnableWebSocketMessageBroker
@Order(Ordered.HIGHEST_PRECEDENCE + 99)
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    private final static Logger logger = LogManager.getLogger(WebSocketConfig.class.getName());

    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    @Autowired
    private PrivateChatService privateChatService;

    private static final String MESSAGE_PREFIX = "/topic";
    private static final String END_POINT = "/chat";
    private static final String APPLICATION_DESTINATION_PREFIX = "/live";


    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        if (registry != null) {
            registry.addEndpoint(END_POINT).setAllowedOrigins("*").withSockJS();
        }
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        if (registry != null) {
            registry.enableSimpleBroker(MESSAGE_PREFIX);
            registry.setApplicationDestinationPrefixes(APPLICATION_DESTINATION_PREFIX);
        }
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.setInterceptors(new ChannelInterceptorAdapter() {

            @Override
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);

                if (StompCommand.CONNECT.equals(accessor.getCommand())) {
                    String authToken = accessor.getFirstNativeHeader("Authentication");
                    String jwt = JwtUtils.resolveToken(authToken);
                    if (jwtTokenProvider.validateToken(jwt)) {
                        Authentication authentication = jwtTokenProvider.getAuthentication(jwt);
                        accessor.setUser(authentication);
                        String itemId = accessor.getFirstNativeHeader("item_id");
                        accessor.setDestination("/topic" + privateChatService.getChannelId(itemId, authentication.getName()));
                        logger.info(accessor.getDestination()); //ex: /topic/chat/3434/chat_with/user3797474342423
                    }
                }
                return message;
            }
        });
    }
}

WebSocketSecurityConfig

的翻译是:

WebSocket安全配置

@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
    @Override
    protected boolean sameOriginDisabled() {
        return true;
    }
}

移动客户端,ng2-stomp-service:

 private _initWebsock(auth_token:string, item_id: number) {
    let headers: Object = {
      Authentication: `Bearer ${auth_token}`,
      item_id: item_id
    };

    this.stomp.configure({
      host :this.websocketApi + 'chat',
      headers: headers,
      queue:{'init':false}
    });

    console.log("Connecting stomp socket...");

    //start connection
    this.stomp.startConnect().then(() => {
      this.stomp.done('init');
      console.log('connected');

      //subscribe
      this.subscription = this.stomp.subscribe(`/chat/${item_id}/`, this.socketListener);
    });
  }
  public socketListener = (data) => {
    console.log(data)
  };

  send(msg: ChatMessage, item_id: number){
    //send data

    console.log(msg);
    this.stomp.send(`/live/chat/${item_id}/send`, {}, JSON.stringify(msg));
  }

问题 1(可能):

在浏览器控制台中显示客户端订阅了 /chat/item_id,而不是 /topic/chat/3434/chat_with/user3797474342423 => 看起来像是 configureClientInboundChannel 没有起作用?

问题 2:

尝试执行 this.stomp.send(/live/chat/${item_id}/send, {}, JSON.stringify(msg)); 时,出现了 o.s.m.s.b.DefaultSubscriptionRegistry : No destination in GenericMessage [payload=byte[2], headers={simpMessageType=MESSAGE.... 错误。


你好,https://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/html/websocket.html 中提到了如何将消息重定向到特定用户,但并不清楚,你也可以参考这个链接:https://stackoverflow.com/questions/30464230/spring-websocket-reply-to-user-message-flow。 - neo_lestat
你能分享一下在发送消息时控制台中的STOMP输出和完整的错误信息吗? - Sergi Almar
1个回答

1

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket-stomp-authentication

https://dev59.com/3ZDea4cB1Zd3GeqPbnGe#33962402

以下是我解决这个问题的方法:

当用户使用Spring Security进行身份验证时,WebSocket模块会基于用户的主体创建唯一频道。例如,“/user/queue/position-updates”会被转换为“/queue/position-updates-user123”。

因此,在客户端上,我所要做的就是订阅/user/queue/requests。

而在服务器端,则使用convertAndSendToUser(request.getFromUser(), "/queue/requests", request)将消息发送到/user/{username}/queue/requests,并由Spring处理其余部分。


我猜如果频道已经创建,那么在控制器注释中这应该可以工作:@SendTo("/topic/chat/{item_id}/chat_with/{buyer_login}"),但是实际上我得到了“GenericMessage中没有目标”的错误,就像主题中所描述的那样。 - user1935987

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接