Authentication Apache HttpClient 4.3.1中使用HTTP隧道/HTTPS连接的抢占式代理身份验证
我试图通过代理发送HTTPS请求,该代理需要使用ApacheHttpClient 4.3.1进行抢占式身份验证 当我没有在第一个请求中直接验证自己时,我的代理会在几分钟内阻止来自IP的连接 我对普通HTTP请求没有任何问题,我只是在请求中手动添加了“代理授权”头 但是当尝试加载HTTPS页面时,HttpClient似乎使用HTTP隧道,因此第一个请求是“CONNECT”命令,然后发送我的实际请求。使用request.setHeader(…)方法不会影响连接请求的头,从而导致“需要HTTP/1.0 407代理身份验证”响应并关闭连接。 之后,HttpClient再次连接,这次添加带有我的凭据的“代理授权”头字段 连接成功(HTTP/1.0 200连接已建立),正在执行我的实际GET请求。 但是当我在那之后再次运行我的程序时,我会得到一个IOException: 信息:I/O异常(java.net.SocketException)在 处理请求:连接重置 在Wireshark中,我可以看到代理不再响应我的“连接”请求(不包含凭据)。 因此,我尝试了几种方法让HttpClient在第一个连接请求中发送凭据: 我将其改编为使用代理,并为代理创建了AuthCache,但它不起作用。 我还尝试将HttpRequestInterceptor添加到我的客户端:Authentication Apache HttpClient 4.3.1中使用HTTP隧道/HTTPS连接的抢占式代理身份验证,authentication,proxy,httpclient,apache-httpclient-4.x,apache-commons-httpclient,Authentication,Proxy,Httpclient,Apache Httpclient 4.x,Apache Commons Httpclient,我试图通过代理发送HTTPS请求,该代理需要使用ApacheHttpClient 4.3.1进行抢占式身份验证 当我没有在第一个请求中直接验证自己时,我的代理会在几分钟内阻止来自IP的连接 我对普通HTTP请求没有任何问题,我只是在请求中手动添加了“代理授权”头 但是当尝试加载HTTPS页面时,HttpClient似乎使用HTTP隧道,因此第一个请求是“CONNECT”命令,然后发送我的实际请求。使用request.setHeader(…)方法不会影响连接请求的头,从而导致“需要HTTP/1.0
static class PreemptiveAuth implements HttpRequestInterceptor {
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
request.setHeader("Proxy-Authorization", "Basic <base64credentials>");
}
}
静态类抢占auth实现HttpRequestInterceptor{
@凌驾
公共无效进程(最终HttpRequest请求、最终HttpContext上下文)抛出HttpException、IOException{
请求.setHeader(“代理授权”、“基本”);
}
}
但这也不会影响“连接”请求。以下是我的代码的其余部分:
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();
}
}
公共类ClientProxyAuthentication{
公共静态void main(字符串[]args)引发IOException、InterruptedException{
HttpHost targetHost=新的HttpHost(“www.google.com”,443,“https”);
HttpHost proxy=新的HttpHost(“,21265,“http”);
CredentialsProvider credsProvider=新的BasicCredentialsProvider();
credsProvider.setCredentials(
新的AuthScope(“,21265),
新用户名密码凭据(“用户名”、“密码”);
CloseableHttpClient httpclient=HttpClients.custom()
.addInterceptorFirst(新的PreemptiveAuth())
.setProxy(代理)
.setProxyAuthenticationStrategy(新的ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(credsProvider).build();
试一试{
HttpGet HttpGet=新的HttpGet(“/”);
setHeader(“代理授权”、“基本”);
System.out.println(“正在执行请求:+httpget.getRequestLine());
System.out.println(“通过代理:+proxy”);
System.out.println(“目标:+targetHost”);
CloseableHttpResponse response=httpclient.execute(targetHost,httpget);
试一试{
HttpEntity=response.getEntity();
System.out.println(“--------------------------------------------------------”;
System.out.println(response.getStatusLine());
如果(实体!=null){
System.out.println(“响应内容长度:+entity.getContentLength());
}
字符串html=EntityUtils.toString(实体,“UTF-8”);
System.out.println(html);
EntityUtils.consume(实体);
}最后{
response.close();
}
}最后{
httpclient.close();
}
}
我怀疑您没有正确初始化身份验证缓存。请尝试此操作
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();
}