2013-04-18 39 views
0

我有一個couchapp,我想通過Play來控制訪問權限!在每個用戶的基礎上。我的計劃是在端口xxxx上託管couchapp,該端口只能在內部訪問,並託管Play!在端口80Play Framework - 使用會話身份驗證的代理請求

在Apache中我會做這樣的,

ProxyPass /couchapp http://localhost:xxxx 
ProxyPassReverse /couchapp http://localhost:xxxx 

但有這種方法不需要認證。我看到Play!有一些代理功能,但我不知道如何添加用戶身份驗證到此,http://www.playframework.com/documentation/2.0/HTTPServer

任何想法如何將用戶身份驗證添加到Play!代理?代碼看起來像這樣。

// 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<PingResponse> 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 
} 

謝謝!

回答

0

我用play.libs.WS使代理調用務實。這是代碼。目前,每次通話都會丟失會話,但這是一個不同的問題。

-Edit - 會話迷路發生是因爲fav.ico沒有發送cookie,Play依賴於會話的cookie。我添加了一個檢查,但最好在路由文件中過濾掉。

package controllers; 

import models.PingResponse; 
import play.data.Form; 
import play.libs.F; 
import play.mvc.Controller; 
import play.mvc.Result; 
import play.libs.WS; 

public class Ping extends Controller { 
    final static String playProxyURL = "http://localhost:9000/"; // pretend this is our proxy domain(should be on port 80) 
    final static String couchAppURL = "http://localhost:80/couchappTest/"; // pretend this is our internal secure site 
    final static String pingURL = "http://localhost:80/pingTest/"; // pretend this is ping endpoint 

    public static Result init() { 
     return Ping.useProxy(""); 
    } 

    public static Result useProxy(String assetPath) { 

     // request for favicon.ico doesn't include cookie :(
     if (assetPath.equals("favicon.ico")) { 
      return ok(); 
     } 
     if (session("authorized") == null || !session("authorized").equals("true")) { 
      System.out.println("not auth"); 
      return redirect(pingURL); 
     } else { 
      return async(
        WS.url(couchAppURL + assetPath).get().map(
          new F.Function<WS.Response, Result>() { 
           public Result apply(WS.Response response) { 
            return ok(response.getBody()).as(response.getHeader("Content-type")); 
           } 
          } 
        ) 
      ); 
     } 
    } 

    public static Result pingCallbackGET(String token, String httpRef) { 
     if (token == null || token.equals("")) { 
      return unauthorized(); 
     } else { 
      System.out.println("auth"); 
      session("authorized", "true"); 
      session("token", token); 
     } 
     return redirect(playProxyURL + httpRef); 
    } 
} 
1

您是否嘗試過加入:

-Dhttp.proxyUser=username -Dhttp.proxyPassword=password 
+1

我更新了問題。我想玩!檢查會話以查看用戶是否已登錄。然後,將用戶引導至couchapp,或要求用戶登錄。 – sissonb