【发布时间】:2015-05-26 06:56:26
【问题描述】:
我正在开发一个 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>
以上没有完全发挥作用。应用程序能够毫无问题地连接到代理服务器,但代理服务器返回 PROXY_AUTH_REQUIRED 错误,这表明 -Dhttp.proxyUser 和 -Dhttp.proxyPassword JVM 选项不起作用。
我搜索并找到了以下两个链接,它们展示了如何在典型的 Java 应用程序中执行此操作。
http://memorynotfound.com/configure-http-proxy-settings-java/ http://rolandtapken.de/blog/2012-04/java-process-httpproxyuser-and-httpproxypassword
按照这两个链接的建议,我在 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
}
通过上述修改,我启动了 Play 应用程序 * 没有指定前面提到的 JVM 选项(因为我现在在代码中给出了相同的选项)。但结果还是一样。代理服务器仍然向应用程序返回 PROXY_AUTH_REQUIRED 错误消息。应用程序再次通过上述代码修改连接到代理服务器,但 Java Authenticator 似乎没有将代理用户名和密码提交给代理服务器。
或者在 Play Java 应用程序中是否有其他方法可以做到这一点?
谢谢
【问题讨论】:
标签: java playframework playframework-2.3