当前位置: 代码迷 >> 综合 >> 使用HttpClient访问Https时发生 SSL:Certificate for <IP> doesn‘t match any of the subject alternative
  详细解决方案

使用HttpClient访问Https时发生 SSL:Certificate for <IP> doesn‘t match any of the subject alternative

热度:10   发布时间:2024-01-05 12:46:18.0

跳过证书认证,信任策略设置为all

import org.apache.http.conn.ssl.TrustStrategy;import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;public class AnyTrustStrategy implements TrustStrategy {@Overridepublic boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {return true;}
}

通过信任策略及SSL获取CloseableHttpClient 

    /*** 解决https访问异常* @return*/public CloseableHttpClient getCloseableHttpClient(){SSLConnectionSocketFactory scsf = null;try {scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new AnyTrustStrategy()).build(),NoopHostnameVerifier.INSTANCE);return HttpClients.custom().setSSLSocketFactory(scsf).build();} catch (NoSuchAlgorithmException e) {log.error(e.getMessage());} catch (KeyManagementException e) {log.error(e.getMessage());} catch (KeyStoreException e) {log.error(e.getMessage());}return null;}

 

 

 

 

参考文章:https://stackoverflow.com/questions/39762760/javax-net-ssl-sslexception-certificate-doesnt-match-any-of-the-subject-alterna

  相关解决方案