Spring Websocket Stomp handle CONNECT frame

1.2k views Asked by At

is there any way to intercept STOMP CONNECT frame in Spring and refuse it under some conditions? The interception itself can be done using SessionConnectEvent but I would like to allow or refuse the connection based on headers. I cannot do it in the SessionConnectEvent listener.

1

There are 1 answers

1
Artem Bilan On BEST ANSWER

If we take a look to the StompSubProtocolHandler code, we'll see this:

try {
    SimpAttributesContextHolder.setAttributesFromMessage(message);
    if (this.eventPublisher != null) {
        if (StompCommand.CONNECT.equals(headerAccessor.getCommand())) {
            publishEvent(new SessionConnectEvent(this, message, user));
        }
........
    outputChannel.send(message);
}

So, the CONNECT frame not only emitted as a SessionConnectEvent, but is sent to the clientInboundChannel as well.

So, what you need to achieve you requirement is just providing a custom ChannelInterceptor with preSend implementation and register it overriding WebSocketMessageBrokerConfigurer.configureClientInboundChannel.