Spring Stomp @SendToUser with unauthenticated user not working

我的梦境 提交于 2019-12-04 09:48:51

问题


I'm trying to respond to an unauthenticated user using @SendToUser.

  • Spring 4.1.1

I'm using a newly created Spring Boot application and the only config I have is:

@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("/topic");
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user"); 
    }
}

Controller code:

@MessageMapping("/search")
@SendToUser("/search")
public String search(@Payload String xxx) {
    return "TEST1234";
}

JS:

var socket = new SockJS('/webapp/stomp');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
    stompClient.subscribe('/user/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=shizav88]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 158 - Processing CONNECT session=shizav88
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/search -> [/search-usershizav88]

JS:

var name = document.getElementById('name').value;
stompClient.send("/app/search", {}, "test"));

Spring output:

DEBUG     o.s.m.s.a.s.SimpAnnotationMethodMessageHandler: 348 - Searching methods to handle SEND /app/search session=shizav88
DEBUG     o.s.m.s.a.s.SimpAnnotationMethodMessageHandler: 446 - Invoking de.benneq.next.search.SearchController#search[1 args]
DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 247 - Returning cached instance of singleton bean 'searchController'
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/shizav88/search -> [/search-usershizav88]

To me it looks like Spring maps the subscription to /search-usershizav88. And then uses /search-usershizav88 to send the response. But my JS client does not receive anything.

What's the problem?


回答1:


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.




回答2:


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...



来源:https://stackoverflow.com/questions/26288166/spring-stomp-sendtouser-with-unauthenticated-user-not-working

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