Create CloseableHttpClient that ignores certificate validation

   In the project, it is necessary to create an http request that ignores the certificate. After searching on the Internet, many of them are large sections of code, and they are not clear.
. I read the java source code, and now I provide the most concise way to create an https request that ignores certificate verification.

/**
* Get the client that ignores certificate verification
 *
 * @return
 * @throws Exception
 */
 public CloseableHttpClient getIgnoeSSLClient() throws Exception {

   SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
      @Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
         return true;
      }
   }).build();

   //创建httpClient
CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).
         setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
   return client;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326572542&siteId=291194637