【问题标题】:How to get endpoint path in ChannelInterceptor with spring boot stomp?如何使用 Spring Boot stomp 在 ChannelInterceptor 中获取端点路径?
【发布时间】:2019-07-08 02:49:21
【问题描述】:

我是 stomp 的新手,使用 spring boot 2.1.2.RELEASE。我有多个端点并配置了ChannelInterceptor 以获取一些信息。

@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());
    }

所有端点都使用myChannelInterceptor(实际上,我希望端点使用自己的ChannelInterceptor),我想通过端点路径在ChannelInterceptor中做事。

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
  if (endpoint.equals("endpoint1")) {
  } else if (endpoint.equals("endpoint2")) {
  }
}

如何在ChannelInterceptor 中获取endpoint 信息?

【问题讨论】:

    标签: spring-boot stomp


    【解决方案1】:

    你可以使用:

    1. 在 IpHandshakeInterceptor 类中将值写入属性映射:

       @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;
       }
      
    2. 在您的 myChannelInterceptor 中从会话属性中读取值:

       @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;
       }
      

    【讨论】:

      【解决方案2】:

      您可以像这样在 SUBSCRIBE 命令中获取目标主题和用户对象(如果您之前填充过它):

      @Override
      public Message<?> preSend(Message<?> message, MessageChannel channel) {
          StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
          if (StompCommand.CONNECT.equals(accessor.getCommand())) {
              // authenticate request and populate user object
          }
          if (StompCommand.SUBSCRIBE.equals(accessor.getCommand())) {
              authorizeRequest(accessor.getUser(), accessor.getDestination());
          }
          return message;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 2019-02-25
        • 2021-08-10
        • 2016-06-29
        相关资源
        最近更新 更多