播放通过身份验证代理访问Web服务的java应用程序

播放通过身份验证代理访问Web服务的java应用程序,java,playframework,playframework-2.3,Java,Playframework,Playframework 2.3,我正在开发一个Play Java应用程序,它需要通过身份验证代理(即代理需要用户名/密码)使用Play WS API(JavaWS)连接到另一个REST服务 首先,在启动Play应用程序时,我尝试使用下面给出的JVM选项 -Dhttp.proxyHost=<proxy_server_hostname> -Dhttp.proxyPort=<proxy_server_port> -Dhttp.proxyUser=<username> -Dhttp.proxyPas

我正在开发一个Play Java应用程序,它需要通过身份验证代理(即代理需要用户名/密码)使用Play WS API(JavaWS)连接到另一个REST服务

首先,在启动Play应用程序时,我尝试使用下面给出的JVM选项

-Dhttp.proxyHost=<proxy_server_hostname> -Dhttp.proxyPort=<proxy_server_port> -Dhttp.proxyUser=<username> -Dhttp.proxyPassword=<password>
-Dhttp.proxyHost=-Dhttp.proxyPort=-Dhttp.proxyUser=-Dhttp.proxyPassword=
上面提到的并没有完全起作用。应用程序能够连接到代理服务器而没有任何问题,但代理服务器返回proxy_AUTH_REQUIRED错误,这表明-Dhttp.proxyUser和-Dhttp.proxyPassword JVM选项不起作用

我搜索并找到了以下两个链接,它们展示了如何在典型的Java应用程序中实现这一点

正如这两个链接中所建议的,我在Global.java中修改了我的Play应用程序的onStart方法,如下所示:

@Override
public void onStart(Application application) {
    //Proxy authentication begin
    System.setProperty("http.proxyHost", "<proxy_server_hostname>");
    System.setProperty("http.proxyPort", "<proxy_server_port>");
    System.setProperty("http.proxyUser", "<username>");
    System.setProperty("http.proxyPassword", "<password>");

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestorType() == RequestorType.PROXY) {
                String prot = getRequestingProtocol().toLowerCase();
                String host = System.getProperty(prot + ".proxyHost", "");
                String port = System.getProperty(prot + ".proxyPort", "");
                String user = System.getProperty(prot + ".proxyUser", "");
                String password = System.getProperty(prot + ".proxyPassword", "");

                if (getRequestingHost().equalsIgnoreCase(host)) {
                    if (Integer.parseInt(port) == getRequestingPort()) {
                        return new PasswordAuthentication(user, password.toCharArray());
                    }
                }
            }
            return null;
        }
    });
    //Proxy authentication end
}
@覆盖
公共void onStart(应用程序){
//代理身份验证开始
System.setProperty(“http.proxyHost”,”);
System.setProperty(“http.proxyPort”,”);
System.setProperty(“http.proxyUser”,即“”);
System.setProperty(“http.proxyPassword”和“”);
setDefault(新验证器(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
if(getRequestorType()==RequestorType.PROXY){
字符串prot=getRequestingProtocol().toLowerCase();
字符串host=System.getProperty(prot+“.proxyHost”,”);
字符串端口=System.getProperty(prot+“.proxyPort”,”);
字符串user=System.getProperty(prot+“.proxyUser”,”);
字符串密码=System.getProperty(prot+“.proxyPassword”,”);
if(getRequestingHost().equalsIgnoreCase(主机)){
if(Integer.parseInt(port)==getRequestingPort()){
返回新密码身份验证(用户,password.tocharray());
}
}
}
返回null;
}
});
//代理身份验证端
}
通过上述修改,我启动了Play应用程序*,而没有指定前面提到的JVM选项(现在我在代码中给出了相同的选项)。但结果还是一样。代理服务器仍然会向应用程序返回proxy_AUTH_REQUIRED错误消息。同样,应用程序通过上述代码修改连接到代理服务器,但Java Authenticator似乎没有向代理服务器提交代理用户名和密码

或者在Play Java应用程序中有不同的方法来实现这一点吗


谢谢

网络身份验证:类身份验证程序

-   The class Authenticator represents an object that knows how to obtain authentication for a network connection. Usually, it will do this by prompting
    the user for information.
- Can be used when credential need to be sent over network.

在您共享的链接中,您似乎还没有阅读完整的描述在我的第二种技术(即以编程方式连接代理)中,我完全完成了您共享的Javadoc链接的其余部分中提到的内容。这是我们应该以编程方式连接到身份验证代理的方式,但它在Play Java中不起作用。谢谢,我阅读了它并使用它将SOAP WS,它对我来说非常适合,即使在代理中也是如此。在我的例子中,在设置系统属性时发现了一些问题,但已成功解决。您是否尝试使用Play Java应用程序?不幸的是,这个答案对我的问题没有用处。因此,我投了反对票。这是你的电话,并检查了属性名一次bcz,这与我在使用SOAP库时面临的相同。