【问题标题】:Routing subscriptions in Spring Websocket STOMP with ActiveMQ使用 ActiveMQ 在 Spring Websocket STOMP 中路由订阅
【发布时间】:2018-10-31 01:28:55
【问题描述】:

我已经用 ActiveMQ 配置了 Spring STOMP,它工作正常。 但是,有没有机会做客户端的订阅路由?

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableStompBrokerRelay("/topic");
    registry.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws")
            .withSockJS();
}

我想要实现的是两个通知队列(在前端) - 一个用于管理员用户,一个用于普通用户。用户不发送任何消息,只接收来自服务器的消息。假设任何用户都可以发送以下订阅请求:

// app is application destination prefix
client.subscribe('/app/notificator', ...);

服务器应该将此请求路由到 ActiveMQ:

/topic/notificator/admin - if logged user is of role admin, or
/topic/notificator/user - if logged user is of role user

如何配置Spring来制定这样的路由策略?

【问题讨论】:

    标签: java spring activemq stomp spring-websocket


    【解决方案1】:

    好的,我用控制器解决了:

    @Controller
    public class QueueController {
    
        @SubscribeMapping("/notificator")
        public String getNotificatorQueue(Principal principal) {
            String role = // get role from principal
            return "/topic/notificator/" + role;
        }
    
    }
    

    这样我可以获得用户订阅的 url,我将在“/app/notificator”响应中调用它。

    【讨论】:

    • 您是否将此响应用作目标并将其传递给 stompclient.subscribe() ?如果是这样,这是不安全的,因为客户端代码可以在订阅之前轻松更改目的地
    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 2022-12-15
    • 2018-11-05
    • 2023-03-17
    • 2012-11-03
    • 2013-01-16
    • 2019-02-14
    • 2015-04-06
    相关资源
    最近更新 更多