public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() {
try {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
SSLContext sslcontext = getSslContent2();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory).build();
// 开始设置连接池
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
// 最大连接数500
poolingHttpClientConnectionManager.setMaxTotal(500);
// 同路由并发数100
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
// 重试次数
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true));
HttpClient httpClient = httpClientBuilder.build();
// httpClient连接配置
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 连接超时
clientHttpRequestFactory.setConnectTimeout(15000);
// 数据读取超时时间
clientHttpRequestFactory.setReadTimeout(30000);
// 连接不够用的等待时间
clientHttpRequestFactory.setConnectionRequestTimeout(20000);
return clientHttpRequestFactory;
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
log.error("初始化HTTP连接池出错", e);
} catch (Exception e) {
log.error("初始化HTTP连接池出错", e);
}
return null;
}
private SSLContext getSslContent1() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
ClassPathResource classPathResource = new ClassPathResource("/cert-test.keystore");
keyStore.load(classPathResource.getInputStream(), "123456".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
SSLContext sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, trustManagers, null);
return sslcontext;
}
private SSLContext getSslContent2() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
ClassPathResource classPathResource = new ClassPathResource("/cert-test.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = null;
try {
cert = cf.generateCertificate(classPathResource.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
boolean flag = false;
for (X509Certificate certificate : arg0){
if(Objects.equals(cert.getPublicKey(), certificate.getPublicKey())){
flag = true;
break;
}
}
return flag;
}
}).build();
return sslContext;
}
网友评论