title: Play Framework - Proxy request with session authentication
body: <p>I have a couchapp that I want to control access to with Play! on a per user basis. My plan is to host the couchapp on port xxxx, which is only accessable internally, and host Play! on port 80. </p>
  
  <p>In Apache I would do it like this, </p>
  
  <pre><code>ProxyPass /couchapp http://localhost:xxxx
  ProxyPassReverse /couchapp http://localhost:xxxx
  </code></pre>
  
  <p>But there is no authentication with this approach. I see Play! has some proxy features, but I don't see anyway to add user authentication to this, <a href="http://www.playframework.com/documentation/2.0/HTTPServer" rel="nofollow">http://www.playframework.com/documentation/2.0/HTTPServer</a></p>
  
  <p>Any idea how to add user authentication to a Play! proxy? The code would look something like this.</p>
  
  <pre><code>// Routes all request to http://localhost:xxxx/ if authenticated
  public static Result useProxy() {
      if (!session("authorized").equals("true")) {
          String pingURL = "";
          return redirect(pingURL); // will call pingCallback after login
      }
      return ok(); // take the original request to /couchapp/xxxx.asset and proxy it to http://localhost:xxxx/xxxx.asset
  }
  
  public static Result pingCallback() {
      Form&lt;PingResponse&gt; pingResponseForm = Form.form(PingResponse.class);
      PingResponse pingResponse = pingResponseForm.bindFromRequest().get();
      if (!pingResponse.isAuthorized()) {
          return unauthorized();
      } else {
          session("authorized", "true");
      }
      return ok(); // take the original request to /couchapp/xxxx.asset and proxy it to http://localhost:xxxx/xxxx.asset
  }
  </code></pre>
  
  <p>Thanks!</p>