简体   繁体   中英

Spring websocket STOMP Unsubscribe from eventHandler

I have a Spring Websocket Stomp application that accepts SUBSCRIBE requests.

In application I have a handler for SUBSCRIBE, that is,

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

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?

  • What else could be used from this event handler to cancel the subscription?

You need to create you ChannelInterceptor implementation. Just extend ChannelInterceptorAdapter and override 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM