我正在开发一个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应用程序中有不同的方法吗?
由于
答案 0 :(得分:0)
网络身份验证:类身份验证器
- 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.