自签名证书绕过不起作用

Kou*_*Ray 2 java ssl apache-httpclient-4.x

CloseableHttpClient client = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
Run Code Online (Sandbox Code Playgroud)

给我一个错误:

[sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
Run Code Online (Sandbox Code Playgroud)

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径

即它没有按预期添加所有自签名证书..

我的 HTTPClient 版本是 4.5.1,HTTPCore 是版本 4.4.4

请给我一个不使用 SSLContextBuilder 等不推荐使用的方法的解决方案

use*_*211 5

尝试使用这个片段:

import java.security.*;
import org.apache.http.conn.ssl.*;


try
{
  SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException
    {
      return true;
    }
  }).build();

  CloseableHttpClient client =HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier())
                              .build();
 }
 catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e)
 {
   e.printStackTrace();
 }
Run Code Online (Sandbox Code Playgroud)