I am new in stomp use spring boot 2.1.2.RELEASE. I have multi endpoint and config a ChannelInterceptor to get some info.
@Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/endpoint1")
                .addInterceptors(new IpHandshakeInterceptor())
                .setAllowedOrigins(origin)
                .withSockJS();
        registry.addEndpoint("/endpoint2")
                .addInterceptors(new IpHandshakeInterceptor())
                .setAllowedOrigins(origin)
                .withSockJS();
        // other andpoint
    }
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(myChannelInterceptor());
    }
All endpoint use myChannelInterceptor(actually, i want endpoint use its own ChannelInterceptor), i want do thing in ChannelInterceptor by endpoint path.
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
  if (endpoint.equals("endpoint1")) {
  } else if (endpoint.equals("endpoint2")) {
  }
}
How can i get endpoint info in ChannelInterceptor?
You can use:
In class IpHandshakeInterceptor write value to attributes map:
 @Override
 public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
     if (serverHttpRequest instanceof ServletServerHttpRequest) {
         ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
         HttpSession session = servletRequest.getServletRequest().getSession();
         //add value to session attributes
         map.put("endpoint", servletRequest.getURI().getPath());
     }
     // ... your logic ...
     return true;
 }
In your myChannelInterceptor read value from session attributes:
 @Override
 public Message<?> preSend(final Message<?> message, final MessageChannel channel) throws AuthenticationException {
     final StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
     String endpoint=accessor.getSessionAttributes().get("endpoint").toString();
     // ... your logic ...
     return message;
 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With