java 使用 HttpProxy 通过抢先式身份验证连接到主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5678000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:15:00  来源:igfitidea点击:

Using HttpProxy to connect to a host with preemtive authentication

javaproxyhttpclientbasic-authentication

提问by Espen Herseth Halvorsen

I am using HttpClient to connect to a host which requires BasicAUTH. But the proxy doesn't require any authentication. I have set it up as follows:

我正在使用 HttpClient 连接到需要 BasicAUTH 的主机。但是代理不需要任何身份验证。我已将其设置如下:

private final HttpClient httpClient; // Spring injected

Setting Basic auth:

设置基本身份验证:

private void setBasicAuth(final String username, final String password) {
    httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
    httpClient.getParams().setAuthenticationPreemptive(true);
}

Setting proxy:

设置代理:

private void setProxy(final String proxyHost, final int proxyPort) {
    hostConfiguration hostConfiguration = httpClient.getHostConfiguration();
    hostConfiguration.setProxy(proxyHost, proxyPort);
}

But I get the following warnings when running the code. Everything works, but I want to get rid of the warnings as well (or at least understand why they appears)

但是我在运行代码时收到以下警告。一切正常,但我也想摆脱警告(或至少了解它们出现的原因)

WARN  o.a.c.httpclient.HttpMethodDirector - Required proxy credentials not available for BASIC <any realm>@proxy.XXXXXX.no:3128
WARN  o.a.c.httpclient.HttpMethodDirector - Preemptive authentication requested but no default proxy credentials available

Any ideas?

有任何想法吗?

回答by Will Iverson

Here's an example from the Apache site for a proxy w/o credentials:

以下是来自 Apache 站点的无凭据代理示例:

http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java

http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java

(From http://hc.apache.org/httpcomponents-client-ga/examples.html)

(来自http://hc.apache.org/httpcomponents-client-ga/examples.html

You are getting the error because you are passing in a username/password and don't need to.

您收到错误是因为您传入了用户名/密码并且不需要。

The WARN messages are coming from the logger (http://hc.apache.org/httpcomponents-client-ga/logging.html) - depending on how you have your logger set up you couldjust ignore that.

WARN 消息来自记录器 (http://hc.apache.org/httpcomponents-client-ga/logging.html) - 根据您设置记录器的方式,您可以忽略它。

Having spent WAY too much time dealing with trying to make a Java application deal with proxy servers, I can tell you that using a tool such as Proxifier ( http://www.proxifier.com/for Mac OS X and Windows) or CNTLM ( http://cntlm.sourceforge.net/) was much easier, more flexible, easier to debug, and kept the code clean.

花了太多时间试图让 Java 应用程序处理代理服务器,我可以告诉你,使用 Proxifier(http://www.proxifier.com/for Mac OS X 和 Windows)或 CNTLM 等工具( http://cntlm.sourceforge.net/) 更容易、更灵活、更容易调试,并保持代码干净。