【问题标题】:How do I provide create a CloseableHttpPipeliningClient that doesn't validate SSL certificates?如何提供创建不验证 SSL 证书的 CloseableHttpPipeliningClient?
【发布时间】:2017-10-31 23:01:53
【问题描述】:

我正在尝试使用支持 HTTP 流水线的 Apache HttpAsyncClient。我想添加一个与Ignoring SSL certificate in Apache HttpClient 4.3 (以及许多其他地方)讨论的内容类似的全信任 SSL 策略。但我看不到任何使用自定义 SSLContext 获得流水线客户端构建器的方法:HttpAsyncClients.createPipelining() 方法不返回构建器,HttpAsyncClients.custom() 构建器不构建 ClosableHttpPipeliningClient

【问题讨论】:

  • 只是...不要。很难做到这一点是有原因的。这是为了防止人们编写破坏互联网安全的代码。咬紧牙关,设置必要的证书。
  • 这是针对测试环境中的测试客户端的。到目前为止,我遇到的每个 HTTP 客户端都存在此选项,包括 Apache HttpClient、cURL、wget 和所有浏览器。很难想象这种限制是故意存在的。

标签: java http apache-httpclient-4.x apache-httpcomponents apache-httpasyncclient


【解决方案1】:

这应该可以满足你的需求

DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT);
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {
    @Override
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
    }
}).build();
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(
        ioReactor,
        RegistryBuilder.<SchemeIOSessionStrategy>create()
                .register("http", NoopIOSessionStrategy.INSTANCE)
                .register("https", new SSLIOSessionStrategy(sslContext))
                .build());
CloseableHttpPipeliningClient httpClient = HttpAsyncClients.createPipelining(cm);
httpClient.start();

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2010-09-16
    • 2017-06-13
    • 2019-03-29
    相关资源
    最近更新 更多