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