[英]Spring websocket STOMP Unsubscribe from eventHandler
I have a Spring Websocket Stomp application that accepts SUBSCRIBE requests.我有一个接受订阅请求的 Spring Websocket Stomp 应用程序。
In application I have a handler for SUBSCRIBE, that is,在应用程序中,我有一个 SUBSCRIBE 处理程序,即,
@Component
public class SubscribeStompEventHandler implements ApplicationListener<SessionSubscribeEvent> {
@Override
public void onApplicationEvent(SessionSubscribeEvent event) {}
}
that I use to validate subscription.我用来验证订阅。
In case if subscription is invalid, for instance, current user can not see that subscription, I would like Broker (I use SimpleMessagingBroker) to "forget" that subscription, or preferably, do not register it at all.如果订阅无效,例如,当前用户看不到该订阅,我希望 Broker(我使用 SimpleMessagingBroker)“忘记”该订阅,或者最好不要注册它。
My questions are:我的问题是:
Can I make Broker to not register the subscription, if I move handling of subscription request to incoming message interceptor and stop message propagation?如果我将订阅请求的处理转移到传入消息拦截器并停止消息传播,我可以让 Broker 不注册订阅吗?
What else could be used from this event handler to cancel the subscription?此事件处理程序还可以使用什么来取消订阅?
You need to create you ChannelInterceptor
implementation.您需要创建
ChannelInterceptor
实现。 Just extend ChannelInterceptorAdapter
and override preSend(Message<?> message, MessageChannel channel)
.只需扩展
ChannelInterceptorAdapter
并覆盖preSend(Message<?> message, MessageChannel channel)
。 Here you will get access to headers with session information for validation.在这里,您可以访问带有会话信息的标头以进行验证。 Also you need to registrate your interceptor
你还需要注册你的拦截器
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.configureBrokerChannel().interceptors(new YourInterceptor())
registry.enableSimpleBroker("/queue/", "/topic/");
registry.setApplicationDestinationPrefixes("/app");
}
More information here How to reject topic subscription based on user rights with Spring-websocket此处的更多信息如何使用 Spring-websocket 根据用户权限拒绝主题订阅
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.