Apache HttpClient 4.3.1中的HTTP隧道/HTTPS连接具有预先代理身份验证功能

5
我试图通过 Apache HttpClient 4.3.1 使用预认证的代理发送 HTTPS 请求。当我在第一个请求中没有直接进行身份验证时,我的代理会阻止来自我的 IP 的连接数几分钟。
在正常的 HTTP 请求中,我没有任何问题,只需手动向请求添加“Proxy-Authorization”头即可。但是,在尝试加载 HTTPS 页面时,HttpClient 似乎使用了 HTTP 隧道,因此第一个请求是“CONNECT”命令,之后才发送我的实际请求。使用 request.setHeader(...) 方法不会影响 CONNECT 请求的头部,导致出现“HTTP/1.0 407 Proxy Authentication Required”响应并关闭我的连接。之后,HttpClient 再次连接,这次会添加带有我的凭据的“Proxy-Authorization”头字段。
连接成功(HTTP/1.0 200 Connection established),我的实际 GET 请求正在被执行。但是,当我再次运行我的程序时,我会收到一个 IOException:
在Wireshark中,我可以看到代理不再响应不包含凭据的“CONNECT”请求。因此,我尝试了几种方法来让HttpClient在第一个CONNECT请求中发送凭据: 我改编了example以使用代理,并为代理创建了AuthCache,但它没有起作用。 我还尝试向我的客户端添加HttpRequestInterceptor:
static class PreemptiveAuth implements HttpRequestInterceptor {
    @Override
    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
        request.setHeader("Proxy-Authorization", "Basic <base64credentials>");
    }
}

但是这不会影响“CONNECT”请求。以下是我的其余代码:
public class ClientProxyAuthentication {

public static void main(String[] args) throws IOException, InterruptedException {
    HttpHost targetHost = new HttpHost("www.google.com", 443, "https");
    HttpHost proxy = new HttpHost("<proxy-ip>", 21265, "http");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope("<proxy-ip>", 21265),
            new UsernamePasswordCredentials("username", "pass"));

    CloseableHttpClient httpclient = HttpClients.custom()
            .addInterceptorFirst(new PreemptiveAuth())
            .setProxy(proxy)
            .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
            .setDefaultCredentialsProvider(credsProvider).build();


    try {

        HttpGet httpget = new HttpGet("/");
        httpget.setHeader("Proxy-Authorization", "Basic <base64credentials>");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("via proxy: " + proxy);
        System.out.println("to target: " + targetHost);

        CloseableHttpResponse response = httpclient.execute(targetHost, httpget);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            String html = EntityUtils.toString(entity, "UTF-8");
            System.out.println(html);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}
1个回答

5
我猜测您没有正确初始化认证缓存,请尝试以下步骤。
HttpHost proxy = new HttpHost("proxy", 8080);

BasicScheme proxyAuth = new BasicScheme();
// Make client believe the challenge came form a proxy
proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=default"));
BasicAuthCache authCache = new BasicAuthCache();
authCache.put(proxy, proxyAuth);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
        new AuthScope(proxy),
        new UsernamePasswordCredentials("username", "password"));

HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
context.setCredentialsProvider(credsProvider);

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    CloseableHttpResponse response = httpclient.execute(new HttpGet("/stuff"), context);
    try {
        // ...
    } finally {
        response.close();
    }
} finally {
    httpclient.close();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接