我有一个在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();


请帮助如何进行...谢谢

最佳答案

经过一些调试后,我发现如果在useSystemProperties()函数中设置connectionManager,调用.build()不会产生任何影响,只有在if条件下,才能检查connectionManager是否已设置(请参见下图)。未设置,它将使用systemProperties(由于指定连接管理器是关于如何进行远程调用的非常低级的自定义,我们应该在PoolingHttpClientConnectionManager初始化本身中进行设置)

PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new
PoolingHttpClientConnectionManager(RegistryBuilder
            .create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", SSLConnectionSocketFactory.getSystemSocketFactory())
            .build());


SSLConnectionSocketFactory.getSystemSocketFactory()是最重要的

java - useSystemProperties()不适用于Apache HttpClientBuilder.create()4.5.6版-LMLPHP

07-24 18:46