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

我不是女神ヾ 2022-12-31 07:30 187阅读 0赞

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

  1. import org.apache.http.conn.ssl.TrustStrategy;
  2. import java.security.cert.CertificateException;
  3. import java.security.cert.X509Certificate;
  4. public class AnyTrustStrategy implements TrustStrategy {
  5. @Override
  6. public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  7. return true;
  8. }
  9. }

通过信任策略及SSL获取CloseableHttpClient

  1. /**
  2. * 解决https访问异常
  3. * @return
  4. */
  5. public CloseableHttpClient getCloseableHttpClient(){
  6. SSLConnectionSocketFactory scsf = null;
  7. try {
  8. scsf = new SSLConnectionSocketFactory(
  9. SSLContexts.custom().loadTrustMaterial(null, new AnyTrustStrategy()).build(),
  10. NoopHostnameVerifier.INSTANCE);
  11. return HttpClients.custom().setSSLSocketFactory(scsf).build();
  12. } catch (NoSuchAlgorithmException e) {
  13. log.error(e.getMessage());
  14. } catch (KeyManagementException e) {
  15. log.error(e.getMessage());
  16. } catch (KeyStoreException e) {
  17. log.error(e.getMessage());
  18. }
  19. return null;
  20. }

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

发表评论

表情:
评论列表 (有 0 条评论,187人围观)

还没有评论,来说两句吧...

相关阅读