Play Framework - 具有会话身份验证的代理请求

时间:2013-04-18 19:55:48

标签: java authentication proxy playframework-2.0

我有一个couchapp,我想控制Play的访问权限!在每个用户的基础上。我的计划是在端口xxxx上托管couchapp,它只能在内部访问,并且主机播放!在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
}

谢谢!

2 个答案:

答案 0 :(得分:1)

您是否尝试过添加:

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

答案 1 :(得分: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);
    }
}
相关问题