4

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?

onkami
  • 8,791
  • 17
  • 90
  • 176
  • http://stackoverflow.com/questions/21554230/how-to-reject-topic-subscription-based-on-user-rights-with-spring-websocket – jahra Sep 22 '16 at 14:39

1 Answers1

5

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

Marv
  • 3,517
  • 2
  • 22
  • 47
jahra
  • 1,173
  • 1
  • 16
  • 41
  • Looks good. Do you know if i return null from preSend and do not throw, it will be the same effect? – onkami Sep 22 '16 at 14:50
  • You can look on `AbstractMessageChannel.ChannelInterceptorChain.applyPreSend` method. It just sets flag to false if there is no message comes from interceptor. You better try it because I don't know what will happen.@AskarIbragimov – jahra Sep 22 '16 at 14:58