Spring Stomp @SendToUser with unauthenticated user not working

六眼飞鱼酱① 提交于 2019-12-03 04:05:43

Now I got it working, but I don't really know WHY:

Config:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp").withSockJS();
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/queue", "/topic"); // <- added "/queue"
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user"); 
    }
}

Controller:

@MessageMapping("/search")
@SendToUser // <- maps to "/user/queue/search"
public String search(@Payload String xxx) {
    return "TEST1234";
}

JS:

stompClient.subscribe('/user/queue/search', function(data){
    alert(data.body);
});

Spring output:

DEBUG  org.springframework.web.servlet.DispatcherServlet: 996 - Successfully completed request
DEBUG   o.s.w.s.handler.LoggingWebSocketHandlerDecorator:  45 - New WebSocketServerSockJsSession[id=fkbmnpkj]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 158 - Processing CONNECT session=fkbmnpkj
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/queue/search -> [/queue/search-userfkbmnpkj]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 175 - Processing SUBSCRIBE /queue/search-userfkbmnpkj id=sub-0 session=fkbmnpkj

Now there's an additional line in the log, which says that it is Processing SUBSCRIBE /queue/search-userfkbmnpkj id=sub-0 session=fkbmnpkj

That was missing before.

It would still be nice, if someone could explain why I need the /queue mapping for this to work properly.

Igor Cherepov

In first post you subscribe at "/user/search". In second "/user/queue/search". I checked "/user/topic/search" and it also works. So solution is to use valid topic name. No magic...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!