Spring Stomp @SendToUser with unauthenticated user

2020-05-29 11:15发布

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?

2条回答
beautiful°
2楼-- · 2020-05-29 11:22

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-05-29 11:32

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

查看更多
登录 后发表回答