簡體   English   中英

Apache HttpClient 4.1 - 代理身份驗證

[英]Apache HttpClient 4.1 - Proxy Authentication

在使用 Apaches HttpComponent 的 httpclient 時,我一直在嘗試從配置的屬性中配置用於代理身份驗證的用戶和密碼,但沒有成功。 我發現的所有示例都引用了不再可用的方法和類,例如HttpStatesetProxyCredentials

那么,誰能給我一個如何配置代理憑據的示例?

對於任何尋找 4.3 答案的人......它相當新,他們的例子沒有使用新的 HttpClientBuilder......所以這就是我在那個版本中實現它的方式:

NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName );

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds );
HttpClientBuilder clientBuilder = HttpClientBuilder.create();

clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort()));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

CloseableHttpClient client = clientBuilder.build();

對於 Basic-Auth,它看起來像這樣:

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

AFAIK NTLM 不支持開箱即用。 但是您可以使用NTCredentials並重載DefaultProxyAuthenticationHandler來管理它。

可以在 4.3+ httpClient 上使用普通的舊用戶名和密碼代替 NTLM,如下所示:

HttpHost proxy = new HttpHost("x.x.com",8080);
Credentials credentials = new UsernamePasswordCredentials("username","password");
AuthScope authScope = new AuthScope("x.x.com", 8080);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope, credentials);
HttpClient client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
HttpResponse response=client.execute(new HttpGet("http://stackoverflow.com/questions/6962047/apache-httpclient-4-1-proxy-authentication"));

如何使用 Apache 的 httpclient 設置代理身份驗證

(代理網絡上的預授權)

此答案使用 Apache 的 HttpClient v4.1 及更高版本。

接受的答案對我不起作用,但我發現了其他有用的答案!

下面是一些來自 apache 的經過測試和驗證的代碼,它們演示了如何通過代理對 HTTP 請求進行身份驗證。

完整文檔位於: https : //hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

這里還有一個來自 Apache 的優秀示例: https : //hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java

  • my_username替換為您的代理用戶名
  • my_password替換為您的代理密碼
  • proxy.mycompany.com替換為您的代理主機
  • 8080替換為您的代理端口
  • 替換google.com與您要發送的HTTP請求該網站的主機。
  • /some-path替換為您要將 HTTP 請求發送到的路徑。 這將使用您之前指定的主機站點 (google.com)。

以下示例將驗證username:password@proxy.mycompany.com:8080並向http://www.google.com/some-path發送GET請求,並將打印響應 HTTP 代碼。

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope("proxy.mycompany", 8080),
            new UsernamePasswordCredentials("my_username", "my_password"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).build();
    try {
        //Replace "google.com" with the target host you want to send the request to
        HttpHost target = new HttpHost("google.com", 80, "http");
        HttpHost proxy = new HttpHost("proxy.mycompany", 8080);

        RequestConfig config = RequestConfig.custom()
            .setProxy(proxy)
            .build();
        CloseableHttpResponse response = null;

        //Replace "/some-path" with the path you want to send a get request to.
        HttpPost httppost = new HttpPost("/some-path");
        httppost.setConfig(config);
        response = httpclient.execute(target, httppost);

        try {
            System.out.println("Return status code is "+response.getStatusLine().getStatusCode());          
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

對 NTLM 來說,一件更簡單的事情對我有用:

httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(proxy_host, proxy_port), 
                    new NTCredentials(this.proxy_user, this.proxy_pass, this.proxy_host, this.proxy_domain));
HttpHost proxy = new HttpHost(this.proxy_host, this.proxy_port, "http");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

對於 HttpClient 4.5 和每個請求身份驗證:

HttpContext httpContext = new BasicHttpContext();
AuthState authState = new AuthState();

authState.update(new BasicScheme(), new UsernamePasswordCredentials("userName", "password"));
httpContext.setAttribute(HttpClientContext.PROXY_AUTH_STATE, authState);
CloseableHttpResponse httpResponse = httpClient.execute(httpRequest, httpContext);

如果你必須讓你的代碼在 4.1 上工作或者想使用下面的代碼片段,重要的是要知道 httpclient 4.1 不會將身份驗證發送到代理。 您可能會收到 407“需要代理身份驗證”狀態代碼。 升級到 4.3.3並且一切正常,盡管 DefaultHttpClient 和 ConnRoutePNames 在此版本中已棄用。 希望這可以幫助!

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM