title: Access Http headers in Spring WebSocket Service
body: <p>I am trying to access the HTTP headers (from the original https upgrade request) in a spring websocket service.</p>
  
  <p>As I understand it I should be able to do this with a HandshakeInterceptor.  I have an interceptor like:</p>
  
  <pre><code>public class WebsocketHandshakeInterceptor implements HandshakeInterceptor {
  
    public WebsocketHandshakeInterceptor() {
    }
  
    @Override
    public boolean beforeHandshake(
        final ServerHttpRequest request,
        final ServerHttpResponse response,
        final WebSocketHandler wsHandler,
        final Map&lt;String, Object&gt; attributes) throws Exception {
      if (request instanceof ServletServerHttpRequest) {
        ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
      }
      return true;
    }
  
    @Override
    public void afterHandshake(
        final ServerHttpRequest request,
        final ServerHttpResponse response,
        final WebSocketHandler wsHandler,
        final Exception exception) {
    }
  }
  </code></pre>
  
  <p>But both the <code>request</code> and <code>servletRequest</code> objects have null headers.  Note that I am not after a session cookie, as most of the example, but other headers.</p>
  
  <p>I have a test client which makes a request like this:</p>
  
  <pre><code>  WebSocketClient client = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(client);
  
    StompSessionHandler sessionHandler = new TestClientStompSessionHandler(getObjectMapper());
  
    final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    if (StringUtils.isNotBlank(token)) {
      LOGGER.error("Adding header           Authorization: Bearer " + token);
      headers.add("Authorization", "Bearer " + token);
    }
  
    headers.add("X-Foo", "bar");
  
    stompClient.connect(url, headers, sessionHandler);
  </code></pre>
  
  <p>Pretty sure it is adding the headers because if I run via my API Gateway which requires the bearer token then it works (with a valid token)</p>