title: Spring Boot Raw WebSocket Set Principal
body: <p>I want to develop notification sender in real time with Spring Boot but I can't use Stomp and sockjs, so I need implement raw WebSocket but I can't find out how to set Principal in WebSocket connection beacuse I want to Authenticate with JWT token. So, where or how can I set principal.
  I'am using these;</p>
  
  <p>WebSocketConfig.java :</p>
  
  <pre><code>@EnableWebSocket
  @Configuration
  public class WebSocketConfig implements WebSocketConfigurer {
  
    @Autowired
    WebSocketNotificationSenderService senderService;
  
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry
                                                webSocketHandlerRegistry) {
  
       webSocketHandlerRegistry.addHandler(createHandler(),
              "/handler").addInterceptors(new HttpSessionHandshakeInterceptor()
       {
          @Override
          public void afterHandshake(ServerHttpRequest request,
                                     ServerHttpResponse response, WebSocketHandler wsHandler,
                                     @Nullable Exception ex) {
  
              super.afterHandshake(request, response, wsHandler, ex);
  
          }
  
          @Override
          public boolean beforeHandshake(ServerHttpRequest request,
                                         ServerHttpResponse response, WebSocketHandler wsHandler,
                                         Map&lt;String, Object&gt; attributes) throws Exception {
  
              return super.beforeHandshake(request, response,
                      wsHandler, attributes)
          }
  
      });
  }
  
  @Bean
  public WebSocketHandler createHandler() {
  
      return new MyHandler(senderService);
  
  }
  }
  </code></pre>
  
  <p>MyHandler.java :</p>
  
  <pre><code>@Component
  public class MyHandler extends TextWebSocketHandler {
  
  WebSocketNotificationSenderService senderService;
  
  public MyHandler(WebSocketNotificationSenderService senderService){
      this.senderService = senderService;
  }
  
  @Override
  public void afterConnectionEstablished(WebSocketSession session) throws Exception {
      senderService.addToSession(session);
  }
  }
  </code></pre>
  
  <p>WebSocketNotificationSenderService.java :</p>
  
  <pre><code>@Service
  public class WebSocketNotificationSenderService implements 
  IWebSocketSenderService&lt;WrapperWsNotification&gt; {
  
  private List&lt;WebSocketSession&gt; sessions = new CopyOnWriteArrayList&lt;&gt;();
  private ObjectMapper mapper = new ObjectMapper();
  
  public void addToSession(WebSocketSession session) {
      sessions.add(session);
  }
  
  @Override
  public void convertAndSend(WrapperWsNotification payload) throws JsonProcessingException {
      String payloadString = mapper.writeValueAsString(payload);
      sessions.stream().forEach(session -&gt; {
          try {
              session.sendMessage(new TextMessage(payloadString));
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      });
  }
  
  @Override
  public void convertAndSendToUser(String user, WrapperWsNotification payload) throws 
    JsonProcessingException {
      String payloadString = mapper.writeValueAsString(payload);
      sessions.forEach(session -&gt; {
          if (session.getPrincipal().getName().equals(user)) {
              try {
                  session.sendMessage(new TextMessage(payloadString));
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      });
  }
  }
  </code></pre>
  
  <p>My Notification Sender to websocket;</p>
  
  <pre><code>@Component
  public class NotificationConsumer {
  
  @Autowired
  WebSocketNotificationSenderService webSocket;
  
  private Logger logger = LoggerFactory.getLogger(NotificationConsumer.class);
  
  public void onReceiveNotification(String object) throws IOException {
      ObjectMapper objectMapper = new ObjectMapper();
      WrapperWsNotification wrapperWsNotification= objectMapper.readValue(object, WrapperWsNotification.class);
      logger.info("User where coming from redis " + wrapperWsNotification.getUser().getUsername());
      webSocket.convertAndSendToUser(wrapperWsNotification.getUser().getUsername(), wrapperWsNotification);
  }
  
  
  }
  </code></pre>