我有一个在Vanilla Spring 4.2.5.RELEASE和JDK 1.7上运行的项目,它使用带有PoolingHttpClientConnectionManager的RestTemplate,并且已经初始化了客户端以使用系统属性
private HttpClient httpClient() {
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(300);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(200);
return HttpClientBuilder
.create()
.useSystemProperties()
.setConnectionManager(poolingHttpClientConnectionManager)
.build();
}
并设置以下系统属性-Dhttps.protocols=TLSv1.2
我对这种行为一无所知,我尝试过创建这样的自定义SSLConnectionSocketFactory
并有效
SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(SSLContexts.createDefault(),
new String[]{"TLSv1.2"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory)
.build());
但是我还有一个要解决的问题,即用于https调用的trustStore,此应用程序已部署在IBM WebSphere中,并且由于httpClient未使用WebSphere服务器设置的systemProperties,因此我应该添加另一个代码段(指向IBM TrustStore) ,我不想这样做,因为它的工作量过多,理想情况下useSystemProperties()应该可以解决这两个问题...
SSLContext sslContext = SSLContexts
.custom()
.loadKeyMaterial(ResourceUtils.getFile(keystoreLocation), null, keystorePassword.toCharArray())
.loadTrustMaterial(ResourceUtils.getFile((truststoreLocation)), truststorePassword.toCharArray())
.build();
请帮助进行操作...谢谢
答案 0 :(得分:0)
经过一些调试后,我发现如果我们在useSystemProperties()
函数中设置connectionManager,则调用.build()
不会产生任何影响,存在一个if条件来检查connectionManager是否已设置(请参考下面的图片),只有在未设置该图片时,它才会使用systemProperties(由于指定连接管理器是关于如何进行远程调用的非常低级的自定义,我们应该在PoolingHttpClientConnectionManager初始化中进行设置本身)
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new
PoolingHttpClientConnectionManager(RegistryBuilder
.create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSystemSocketFactory())
.build());
SSLConnectionSocketFactory.getSystemSocketFactory()
是最重要的