-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathHttpClient5PreemptiveBasicAuthentication.java
59 lines (50 loc) · 2.78 KB
/
HttpClient5PreemptiveBasicAuthentication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.wdbyte.httpclient;
import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;
/**
* 抢先式身份认证
* 注意安全性问题
*/
public class HttpClient5PreemptiveBasicAuthentication {
public static void main(final String[] args) throws Exception {
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
// 生成一个认证信息
final BasicScheme basicAuth = new BasicScheme();
basicAuth.initPreemptive(new UsernamePasswordCredentials("admin", "123456".toCharArray()));
// 认证信息的生效域名信息
final HttpHost target = new HttpHost("http", "httpbin.org", 80);
// 添加认证信息到 HttpClient 请求上下文中
final HttpClientContext localContext = HttpClientContext.create();
localContext.resetAuthExchange(target, basicAuth);
//AuthScope authScope = new AuthScope("httpbin.org", 80);
//BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
//credsProvider.setCredentials(authScope,
// new UsernamePasswordCredentials("admin", "1234556".toCharArray()));
AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
//localContext.setCredentialsProvider(credsProvider);
localContext.setAuthCache(authCache);
final HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/admin/123456");
System.out.println("执行请求 " + httpget.getMethod() + " " + httpget.getUri());
for (int i = 0; i < 3; i++) {
try (final CloseableHttpResponse response = httpclient.execute(httpget, localContext)) {
System.out.println("----------------------------------------");
System.out.println(response.getCode() + " " + response.getReasonPhrase());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
}
}