我有一个在 Tomcat 6 中运行的 Java Web 应用程序,它从远程 URL 加载 RSS 提要。
I use Rome http://java.net/projects/rome/为我处理 RSS 提要和不同的格式。连接部分如下所示:
try{
feedSource = new URL(rssObject.getAsset());
}catch(MalformedURLException mue){
logger.error(...);
throw mue;
}
try{
URLConnection connection = feedSource.openConnection();
feed = new SyndFeedInput().build(new XmlReader(connection));
}catch(Exception){handle...}
该代码工作正常,除了在这个新客户端上,他们使用代理。
为了使用代理,我设置了 http.proxyHost 和 proxyPort 系统属性:
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);
对代理进行 HTTP GET 正常,但现在我收到 HTTP 502 错误(网关错误或类似的错误)。
通过分析与 Wireshark 的 HTTP 交换,我注意到代理需要身份验证。它发送 HTTP 507。Java 正在尝试进行身份验证,但它使用了错误的用户名和密码。好像是用主机名作为用户名,至于密码我不知道。
所以我尝试实现指定用户名+密码的Authenticator方法:
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
logger.info(MessageFormat.format("Generating PasswordAuthentitcation for proxy authentication, using username={0} and password={1}.", username, password));
return new PasswordAuthentication(username, password.toCharArray());
}
});
现在我的问题是它被忽略了。 getPasswordAuthentication 方法永远不会被调用。我在日志文件中没有看到日志记录语句,并且使用 Wireshark 我可以看到它仍然使用主机名作为用户名。
为什么 ?看来java以某种方式尝试在不咨询Authenticator的情况下自行进行身份验证。
代理似乎是使用 NTLM 进行身份验证的 MS 设备。 java中有一些内置的机制来处理这个问题吗?运行该应用程序的计算机是Win Server 2008 R2。