HttpClient是否同时配置了SSL和代理身份验证



我有两段使用HttpClient的代码,
第一部分是在端点需要SSL的情况下
第二部分是具有基本身份验证的代理连接
我的问题是,如果我有SSL+代理或SSL,我该如何使此代码成为条件我很难弄清楚如何设置默认凭据,例如,在我使用SSL部分中的客户端创建客户端之后

.setDefaultCredentialsProvider(credsProvider)

这部分是我如何在需要SSL 时创建客户端

CloseableHttpClient client = null;
if(conf.isUseSslConfig()) {         
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new File(conf.getTrustStoreLocation()), conf.getTrustStorePassword().toCharArray(), new TrustSelfSignedStrategy()).build();
// Allow protocols
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,conf.getTlsVersions(), null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());               
client = HttpClients.custom().setSSLSocketFactory(sslsf).build();

}else {
client= HttpClients.createDefault();                
}

这部分是我如何在需要代理身份验证时创建客户端:

if(conf.isUseProxyConfig()){
CredentialsProvider credsProvider = new BasicCredentialsProvider();        
credsProvider.setCredentials(
new AuthScope("fakeProxy.xerox.com", 80),
new UsernamePasswordCredentials("xeroxUser","fakePassword123"));
HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
}

所以底线是如何使这两个部分一起工作,所以在的情况下

  1. 使用SSL+代理和身份验证进行呼叫
  2. 仅使用SSL进行呼叫
  3. 仅使用代理和身份验证进行调用

您可以通过这种方式编写代码来解决多个条件:

CloseableHttpClient client = null;
if(conf.isUseSslConfig() && conf.isUseProxyConfig()) {         
setSSLSetting(client);
setProxy()
}else  if(conf.isUseSslConfig()) {
setSSLSetting(client);
}else {
client= HttpClients.createDefault();                
}

private void setProxy(){
CredentialsProvider credsProvider = new BasicCredentialsProvider();        
credsProvider.setCredentials(new AuthScope("fakeProxy.xerox.com", 80),new UsernamePasswordCredentials("xeroxUser","fakePassword123"));
}       

private void setSSLSetting(CloseableHttpClient client){
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new File(conf.getTrustStoreLocation()), conf.getTrustStorePassword().toCharArray(), new TrustSelfSignedStrategy()).build();
// Allow protocols
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,conf.getTlsVersions(), null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());               
client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
}

或者,您可以创建返回具有不同设置和配置的客户端的方法,如下所示:


final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
private CloseableHttpClient createHttpClient(String headerName, String value) throws NoSuchAlgorithmException, KeyManagementException,KeyStoreException {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
Header header = new BasicHeader(headerName,value);
List<Header> headers = new ArrayList<>();
headers.add(header);
RequestConfig reqConfig = RequestConfig.custom().setConnectionRequestTimeout(long milli seconds).build();
CloseableHttpClient httpclient = HttpClients.custom().
setDefaultHeaders(headers).
setDefaultRequestConfig(reqConfig).
setConnectionManager(cm).
build();
return httpclient;
}

最新更新