Spring 4.3.6 STOMP / WebSockets与auth-token

时间:2017-05-12 23:46:41

标签: java spring authentication stomp spring-websocket

已解决:请参阅评论

我目前正在尝试使用 x-auth-token 在spring服务器和角度客户端之间打开WebSockets连接。因此,我有以下几段代码:

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.Message; 
import org.springframework.messaging.MessageChannel; 
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; 
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Configuration
@EnableWebSocketMessageBroker
@Order(Ordered.HIGHEST_PRECEDENCE + 99)
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        LOGGER.info("registering websockets");
        registry.addEndpoint("/api/v1/websocket").setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exceptio) {
        LOGGER.info("ERRORORRRRRRRR");
        exception.printStackTrace();
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
      registration.setInterceptors(new ChannelInterceptorAdapter() {

          @Override
          public Message<?> preSend(Message<?> message, MessageChannel channel) {

              StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);

              LOGGER.info("in override");

              if (StompCommand.CONNECT.equals(accessor.getCommand())) {

                  String authToken = accessor.getFirstNativeHeader("x-auth-token");

                  LOGGER.info("Header auth token: " + authToken);

                  // Principal user = ... ; // access authentication header(s)
                  //accessor.setUser(user);
              }

              return message;
          }
      });
    }
}

从我的WebSocketConfig可以看出,我目前还没有真正处理x-auth-token,而是尝试记录一次。最后,我试图遵循Spring文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-authentication-token-based

我遇到的唯一问题是configureClientInboundChannel() preSend()方法似乎没有被调用 - 我无法弄清楚原因。

在客户端,显示以下错误:

client_error_message

通过比较错误日志和registerStompEndpoints()中定义的端点可以看出,实际端点是正确的端点。我仍然不明白为什么LOGGER.info("in override");中的configureClientInboundChannel()没有被调用。

我真的想通过stomp标头而不是 hacky token-as-URL-paramter-solution传输auth-token。这里有人有想法吗?我是否遗漏了已经链接的弹簧文档中的内容?

提前感谢您的帮助!

0 个答案:

没有答案