Apache HttpClient 4.3.1中具有HTTP隧道/ HTTPS连接的抢占式代理身份验证

use*_*986 5 authentication proxy httpclient apache-commons-httpclient apache-httpclient-4.x

我正在尝试通过需要使用Apache HttpClient 4.3.1进行抢先身份验证的代理发送HTTPS请求。

当我在第一个请求中未直接对自己进行身份验证时,代理会阻止与IP的连接几分钟。

我对正常的HTTP请求没有任何问题,只是将“ Proxy-Authorization”标头手动添加到了请求中。

但是,当尝试加载HTTPS页面时,HttpClient似乎使用了HTTP隧道,因此第一个请求是“ CONNECT”命令,然后发送我的实际请求。使用request.setHeader(...)方法不会影响CONNECT请求的标头,从而导致出现“需要HTTP / 1.0 407代理身份验证”响应并关闭我的连接。之后,HttpClient再次连接,这次使用我的凭据添加“ Proxy-Authorization”标头字段。

连接成功(建立了HTTP / 1.0 200连接),并且正在执行我的实际GET请求。但是,之后我再次运行程序时,将得到IOException:

信息:处理请求时捕获了I / O异常(java.net.SocketException):连接重置

在Wireshark中,我可以看到代理不再响应我的“ CONNECT”请求(不包含凭据)。因此,我尝试了几种方法来使HttpClient在第一个CONNECT请求中发送凭据:我修改了此示例以使用代理,并为代理创建了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>");
    }
}
Run Code Online (Sandbox Code Playgroud)

但这也不会影响“ 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

ok2*_*k2c 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();
}
Run Code Online (Sandbox Code Playgroud)