如何在Spring Boot应用程序中向STOMP CREATED消息添加自定义标头?

时间:2017-02-10 18:41:12

标签: spring spring-boot websocket stomp spring-websocket

我正在尝试将自定义标头添加到STOMP'CREATED'消息,该消息由客户端在第一次连接时接收。这是使用STOMP JavaScript连接到WebSocket的函数:

function connect() {
    socket = new SockJS('/chat');
    stompClient = Stomp.over(socket);
    stompClient.connect('', '', function(frame) {
      whoami = frame.headers['user-name'];
      console.log(frame);
      stompClient.subscribe('/user/queue/messages', function(message) {
          console.log("MESSAGE RECEIVED:");
          console.log(message);

        showMessage(JSON.parse(message.body));
      });
      stompClient.subscribe('/topic/active', function(activeMembers) {
        showActive(activeMembers);
      });
    });
  }

此功能将以下内容打印到浏览器的控制台:

body: ""
command: "CONNECTED"
headers: Object
    heart-beat: "0,0"
    user-name: "someuser"
    version: "1.1"

我想添加自定义标题,因此输出必须如下:

body: ""
command: "CONNECTED"
headers: Object
    heart-beat: "0,0"
    user-name: "someuser"
    version: "1.1"
    custom-header: "foo"

我的Spring Boot应用程序中有以下WebSocket配置。

WebSocketConfig.java

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

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

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/chat", "/activeUsers")
            .withSockJS()
            .setInterceptors(customHttpSessionHandshakeInterceptor());
  }

  ...

  @Bean
  public CustomHttpSessionHandshakeInterceptor 
        customHttpSessionHandshakeInterceptor() {
        return new CustomHttpSessionHandshakeInterceptor();

  }

}

我试图注册'HandshakeInterceptor'来设置自定义标头,但它没有用。这是'CustomHttpSessionHandshakeInterceptor':

CustomHttpSessionHandshakeInterceptor.java

public class CustomHttpSessionHandshakeInterceptor implements 

HandshakeInterceptor {

     @Override
        public boolean beforeHandshake(ServerHttpRequest request,
        ServerHttpResponse response,
        WebSocketHandler wsHandler,
        Map<String, Object> attributes) throws Exception {
            if (request instanceof ServletServerHttpRequest) {


                 ServletServerHttpRequest servletRequest =
                    (ServletServerHttpRequest) request;
                 attributes.put("custom-header", "foo");
            }
            return true;
        }

        public void afterHandshake(ServerHttpRequest request,
            ServerHttpResponse response,
            WebSocketHandler wsHandler,
            Exception ex) { }
}

我在https://dzone.com/articles/spring-boot-based-websocket找到了此代码段
有人可以解释一下为什么这种方法不起作用?是否有另一种方法可以在Spring Boot应用程序的服务器端将自定义标头设置为STOMP'CREATED'消息?
谢谢!

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

也许为时已晚,但迟到总比没有好......

服务器消息(例如CONNECTED)是不可变的,意味着它们不能被修改。

我要做的是注册客户端出站拦截器并通过覆盖preSend(...)方法捕获连接的消息,并使用我的自定义标头构建新消息。

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) 
{
    LOGGER.info("Outbound channel pre send ...");
    final StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
    final StompCommand command = headerAccessor.getCommand();
    if (!isNull(command)) {
        switch (command) {
            case CONNECTED:
                final StompHeaderAccessor accessor = StompHeaderAccessor.create(headerAccessor.getCommand());
                accessor.setSessionId(headerAccessor.getSessionId());
                @SuppressWarnings("unchecked")
                final MultiValueMap<String, String> nativeHeaders = (MultiValueMap<String, String>) headerAccessor.getHeader(StompHeaderAccessor.NATIVE_HEADERS);
                accessor.addNativeHeaders(nativeHeaders);

                // add custom headers
                accessor.addNativeHeader("CUSTOM01", "CUSTOM01");

                final Message<?> newMessage = MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
                return newMessage;
            default:
                break;
            }
        }
        return message;
    }

<强> @UPDATE :::

所需的接口称为ChannelInterceptor并注册您自己的实现,您需要添加@Configuration带注释的类

@Configuration
public class CustomMessageBrokerConfig extends WebSocketMessageBrokerConfigurationSupport
implements WebSocketMessageBrokerConfigurer{}

并覆盖方法configureClientOutboundChannel,如下所示

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
    log.info("Configure client outbound channel started ...");
    registration.interceptors(new CustomOutboundChannelInterceptor());
    log.info("Configure client outbound channel completed ...");
}