首页 文章

Apache HttpClient 4.3.1中使用HTTP隧道/ HTTPS连接进行抢占式代理身份验证

提问于
浏览
4

我试图通过代理发送HTTPS请求,该代理需要使用Apache HttpClient 4.3.1进行抢先认证 .

当我没有在第一个请求中直接验证自己时,我的代理会阻止与我的IP连接几分钟 .

我对普通的HTTP请求没有任何问题,我只是手动将“代理授权”标头添加到请求中 .

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

连接成功( Build 了HTTP / 1.0 200连接)并且正在执行我的实际GET请求 . 但是当我在那之后再次运行我的程序时,我会得到一个IOException:

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

在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 回答

  • 4

    我怀疑你没有正确初始化auth缓存 . 请试试这个 .

    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();
    }
    

相关问题