How to close STOMP web site on spring server


I use spring -websocket and spring -messaging (version 4.2.2.RELEASE) to implement STOMP via websockets with a fully-functional broker (Apache ActiveMQ 5.10.0).
My clients are intended for SIGNING only to recipients - that is, they should not be able to send SEND messages. In addition, I would like to have more stringent control over which recipients my customers can subscribe to. In any case (that is, when a client tries to send a message or subscribe to an invalid destination), I would like to be able to

  • send the corresponding error and / or
  • close websocket

Please note that all my recipients are sent to ActiveMQ. I thought I could implement ChannelInterceptor on the inbound channel, but looking at the API, I can’t figure out how to achieve what I want. Is this possible, and what is the best way to test client requests? My websocket configuration below:

<websocket:message-broker
    application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/pushchannel"/>
    <websocket:stomp-broker-relay relay-host="localhost"
        relay-port="61613" prefix="/topic"
        heartbeat-receive-interval="300000" heartbeat-send-interval="300000" />
    <websocket:client-inbound-channel>
        <websocket:interceptors>
            <bean class="MyClientMessageInterceptor"/>
        </websocket:interceptors>
    </websocket:client-inbound-channel>
</websocket:message-broker>
+4
source share
1 answer

You can write an incoming interceptor and send the corresponding error message to the client.

public class ClientInboundChannelInterceptor extends ChannelInterceptorAdapter {

@Autowired
private SimpMessagingTemplate simpMessagingTemplate;

@Override
public Message<?> preSend(Message message, MessageChannel channel) throws IllegalArgumentException{
    StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
    logger.debug("logging command " + headerAccessor.getCommand());
    try {
          //write your logic here
        } catch (Exception e){
            throw new MyCustomException();
        }
    }

}

UPDATE:

1) When you throw any exception from ClientInboundChannelInterceptor, it will be sent as a frame ERROR, you do not need to do anything special.

2) , - DISCONNECT ( ).

SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
headerAccessor.setSessionId(sessionId);
headerAccessor.setLeaveMutable(true);

template.convertAndSendToUser(destination,new HashMap<>(),headerAccessor.getMessageHeaders());

.

1) ClientInboundChannelInterceptor.

2) Handler/Controller @SubscribeMapping .

@SubscribeMapping("your destination")
public ConnectMessage handleSubscriptions(@DestinationVariable String userID, org.springframework.messaging.Message message){
    // this is my custom class
    ConnectMessage frame= new ConnectMessage();
    // write your logic here
    return frame;
}

frame .

0

All Articles