简体   繁体   English

useSystemProperties() 不适用于 Apache HttpClientBuilder.create() 版本 4.5.6

[英]useSystemProperties() is not working with Apache HttpClientBuilder.create() version 4.5.6

I have a project running on Vanilla Spring 4.2.5.RELEASE and JDK 1.7 and it uses RestTemplate with PoolingHttpClientConnectionManager, and I've initialized the client to use system properties我有一个在 Vanilla Spring 4.2.5.RELEASE 和 JDK 1.7 上运行的项目,它使用 RestTemplate 和 PoolingHttpClientConnectionManager,并且我已经初始化了客户端以使用系统属性

private HttpClient httpClient() {
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
        poolingHttpClientConnectionManager.setMaxTotal(300);
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(200);
        return HttpClientBuilder
        .create()
        .useSystemProperties()
        .setConnectionManager(poolingHttpClientConnectionManager)
        .build();
}

and set the following system properties -Dhttps.protocols=TLSv1.2并设置以下系统属性-Dhttps.protocols=TLSv1.2

I'm clueless with this behavior, I've tried creating custom SSLConnectionSocketFactory like this and it worked我对这种行为一无所知,我尝试过像这样创建自定义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());

but I have one more problem to fix which is trustStore for https call, this application is deployed in IBM WebSphere and since the httpClient is not using systemProperties set by WebSphere server I should add another code snippet(to point to IBM TrustStore), which I don't want to do because its too much work and ideally useSystemProperties() should solve both the problems...但我还有一个问题要解决哪个是 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();

Please help on how to proceed...Thanks请帮助如何进行...谢谢

After some debugging I found that calling useSystemProperties() will not have any impact if we're setting the connectionManager, in .build() function, there's an if condition to check if the connectionManager is set( refer the image below ), only if its not set, it will use systemProperties(since specifying connection manager is a much low level customization on how remote calls should be made, we're supposed to set that in PoolingHttpClientConnectionManager initialization itself)经过一些调试,我发现如果我们设置 connectionManager,调用useSystemProperties()不会有任何影响,在.build()函数中,有一个 if 条件来检查是否设置了 connectionManager(请参阅下图),仅当它没有设置,它将使用 systemProperties(因为指定连接管理器是一个关于如何进行远程调用的低级定制,我们应该在 PoolingHttpClientConnectionManager 初始化本身中设置它)

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

SSLConnectionSocketFactory.getSystemSocketFactory() is the most important thing SSLConnectionSocketFactory.getSystemSocketFactory()是最重要的

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM