Search code examples
springstompspring-websocket

Spring Websocket Stomp handle CONNECT frame


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.


Solution

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