title: How to add custom headers to STOMP CREATED message in Spring Boot application?
body: <p>I'm trying to add custom headers to the STOMP 'CREATED' message, which is received by client at the first connection. Here is the function which connects to the WebSocket using STOMP JavaScript:<br/></p>
  
  <pre><code>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);
        });
      });
    }
  </code></pre>
  
  <p>This function prints the following to the browser's console:</p>
  
  <pre><code>body: ""
  command: "CONNECTED"
  headers: Object
      heart-beat: "0,0"
      user-name: "someuser"
      version: "1.1"
  </code></pre>
  
  <p>And i want to add custom header so output must look like:</p>
  
  <pre><code>body: ""
  command: "CONNECTED"
  headers: Object
      heart-beat: "0,0"
      user-name: "someuser"
      version: "1.1"
      custom-header: "foo"
  </code></pre>
  
  <p>I have the following WebSocket configuration in my Spring Boot app.<br/><br/>
  <strong>WebSocketConfig.java</strong></p>
  
  <pre><code>@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();
  
    }
  
  }
  </code></pre>
  
  <p>I have tried to register the 'HandshakeInterceptor' to set custom header, but it didn't work. Here is 'CustomHttpSessionHandshakeInterceptor':<br/>
  <br/>
  <strong>CustomHttpSessionHandshakeInterceptor.java</strong> </p>
  
  <pre><code>public class CustomHttpSessionHandshakeInterceptor implements 
  
  HandshakeInterceptor {
  
       @Override
          public boolean beforeHandshake(ServerHttpRequest request,
          ServerHttpResponse response,
          WebSocketHandler wsHandler,
          Map&lt;String, Object&gt; 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) { }
  }
  </code></pre>
  
  <p>I have found this code snippet at <a href="https://dzone.com/articles/spring-boot-based-websocket" rel="noreferrer">https://dzone.com/articles/spring-boot-based-websocket</a>
  <br/>Can someone explain me why this approach does not work? Is there another way to set custom headers to the STOMP 'CREATED' message at server side in Spring Boot application?
  <br/>Thanks!</p>