title: Spring STOMP Token Based Security
body: <p>I want to implement token based authentication for web socket connection.</p>
  
  <p>My backend system is <strong>server</strong> and a machine is <strong>client</strong>, how to implement authentication during sending and receiving message between machine and server, no user authentication here.</p>
  
  <pre><code>@Configuration
  @EnableWebSocketMessageBroker
  public class MyConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
      registration.setInterceptors(new ChannelInterceptorAdapter() {
  
          @Override
          public Message&lt;?&gt; preSend(Message&lt;?&gt; message, MessageChannel channel) {
  
              StompHeaderAccessor accessor =
                  MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
  
              if (StompCommand.CONNECT.equals(accessor.getCommand())) {
                  String authToken = accessor.getFirstNativeHeader("X-Auth-Token");
                  log.debug("webSocket token is {}", authToken);
                  Principal user = ... ; // access authentication header(s)
                  accessor.setUser(user);
              }
  
              return message;
          }
      });
    }
  }
  </code></pre>
  
  <p>However, I'm totally lost how I would do at "Principal user = ... ;". How would I get Principle with the token? As here no user exist, only communication between a machine and a backend server</p>