Java代理认证

2024-01-03

我有一个在 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。


我们在基于 NTLM 的代理上进行了相同的身份验证。

代理上的身份验证实际上是正常的HTTP 基本身份验证.

我们使用了以下方法:

protected URLConnection newURLConnection(URL pURL) throws IOException {
    URLConnection urlConnection = super.newURLConnection(pURL);

    String auth = new String(Base64.base64Encode(new String("username:password").getBytes()));
    auth = "Basic " + auth;
    urlConnection.setRequestProperty("Proxy-Connection","Keep-Alive");
    urlConnection.setRequestProperty("Proxy-Authorization",auth);
    return urlConnection;
}

与代理 jvm 设置一起,就成功了。

See http://en.wikipedia.org/wiki/Basic_access_authentication http://en.wikipedia.org/wiki/Basic_access_authentication.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java代理认证 的相关文章

随机推荐