title: How can I authenticate user in Play's 2.0 WebSocket?
body: <p>ellou'</p>
  
  <p>I have common Play WebSocket method which is working as expected, but I need to authenticate user. Unfortunately when trying even simplest way to identify user in the method:</p>
  
  <pre><code>public class Application extends Controller {
      public static WebSocket&lt;JsonNode&gt; connect() {
          return new WebSocket&lt;JsonNode&gt;() {
  
              @Override
              public void onReady(final WebSocket.In&lt;JsonNode&gt; in, final WebSocket.Out&lt;JsonNode&gt; out) {
                  User logged = User.findByUsername(Http.Context.current().request().username());
                  // ...
              }
          };
      }
  }
  </code></pre>
  
  <p>I get an error:</p>
  
  <pre><code>java.lang.RuntimeException: There is no HTTP Context available from here.
  </code></pre>
  
  <p>My current workaround is to pass random, unique identifier to view and saving it in User model, than I can connect to the socket from the view and find user with the given identifier, but this is no perfect as there can be only one WebSocket connection per user and also doesn't prevent the spoof.  </p>
  
  <p>How should I authenticate the user and prevent spoof attack? </p>