0

我有一个沙发应用程序,我想通过 Play 控制对它的访问!在每个用户的基础上。我的计划是在端口 xxxx 上托管 couchapp,该端口只能在内部访问,并托管 Play!在 80 端口上。

在 Apache 我会这样做,

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

但是这种方法没有身份验证。我看到了播放!有一些代理功能,但无论如何我都看不到要为此添加用户身份验证,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
}

谢谢!

4

2 回答 2

1

您是否尝试过添加:

-Dhttp.proxyUser=username -Dhttp.proxyPassword=password
于 2013-04-18T19:59:22.047 回答
0

我使用 play.libs.WS 务实地进行代理调用。这是代码。目前,每次通话都会丢失会话,但这是一个不同的问题。

-编辑 - 会话丢失是因为 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);
    }
}
于 2013-04-19T22:23:08.663 回答