title: Secure Spring-Webscoket using spring-security and access principal from websocket message
body: <p>Spring Security is very nice framework widely used for Authentication &amp; Authorization.</p>
  
  <p>I have a requirement in which the application to be authenticated using j_spring_security_check, and only authorized users can make request to websocket handler.</p>
  
  <p>I have configured spring security as per <a href="http://malalanayake.wordpress.com/2014/06/27/spring-security-on-rest-api/" rel="nofollow">http://malalanayake.wordpress.com/2014/06/27/spring-security-on-rest-api/</a></p>
  
  <p>And I have configured websocket as per <a href="http://syntx.io/using-websockets-in-java-using-spring-4/" rel="nofollow">http://syntx.io/using-websockets-in-java-using-spring-4/</a>.</p>
  
  <p>I want <strong>MyPrincipal</strong> principal object to be accessed from handleTextMessage handler as per below:</p>
  
  <pre><code>    @Override
      protected void handleTextMessage(WebSocketSession session,
              TextMessage message) throws Exception {
          System.out.println("Protocol: "+session.getAcceptedProtocol());
          TextMessage returnMessage = new TextMessage(message.getPayload()
                  + " received at server");
          System.out.println("myAttrib="
                  + session.getAttributes().get("myAttrib"));
          MyPrincipal user = (MyPrincipal) ((Authentication) session
                  .getPrincipal()).getPrincipal();
          System.out.println("User: " + user.getUserId());
          session.sendMessage(returnMessage);
      }
  </code></pre>
  
  <p>Please replay ASAP.</p>