当前位置:   article > 正文

ssl忽略证书 SSLHandshakeException:PKIX path building failed ——java client

ssl忽略证书 SSLHandshakeException:PKIX path building failed ——java client

忽略证书的代码 

  1. public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
  2. SSLContext sc = SSLContext.getInstance("TLS");
  3. // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
  4. X509TrustManager trustManager = new X509TrustManager() {
  5. @Override
  6. public void checkClientTrusted(
  7. java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
  8. String paramString) throws CertificateException {
  9. }
  10. @Override
  11. public void checkServerTrusted(
  12. java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
  13. String paramString) throws CertificateException {
  14. }
  15. @Override
  16. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  17. return null;
  18. }
  19. };
  20. sc.init(null, new TrustManager[]{trustManager}, null);
  21. return sc;
  22. }

 将返回值给到httpclient

写法一:

  1. SSLContext ignoreVerifySSL = createIgnoreVerifySSL();
  2. HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
  3. httpClientBuilder.setSSLContext(ignoreVerifySSL); // 设置SSL管理工厂
  4. // ... 设置其他调优参数(比如连接池大小等)
  5. CloseableHttpClient httpClient = httpClientBuilder.build();

写法二: 

  1. SSLContext ignoreVerifySSL = createIgnoreVerifySSL();
  2. CloseableHttpClient httpClient = HttpClients.custom()
  3. // .setConnectionManager(connectionManager)
  4. .setKeepAliveStrategy(myStrategy)
  5. .setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build())
  6. .setSSLContext(ignoreVerifySSL)
  7. .build();

后续写法:创建连接,拿到response返回值

  1. try (CloseableHttpClient closeableHttpClient = httpClientBuilder.build()) {
  2. HttpEntity entity = new StringEntity(json, "UTF-8");
  3. HttpPost post = new HttpPost(url);
  4. post.setEntity(entity);
  5. post.setHeader("Content-type", "application/json");
  6. HttpResponse response = closeableHttpClient.execute(post);
  7. result = EntityUtils.toString(response.getEntity(), "UTF-8");
  8. System.out.println(result);
  9. return result;
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }

注意:千万不要使用自定义的ConnectionManager,否则会导致SSL管理工厂失效,无法跳过SSL证书认证。

  1. // 千万别设置这个参数!!
  2. httpClientBuilder.setConnectionManager(httpClientConnectionManager);

原因:HttpClientBuilder中有一段代码,只有当自定义的ConnectionManager为空时,才会使用SSL管理工厂或者sslcontext,否则,不会生效。

  1. public CloseableHttpClient build() {
  2. final HttpClientConnectionManager connManagerCopy = this.connManager;
  3. Object reuseStrategyCopy;
  4. Object proxyAuthStrategyCopy;
  5. if (connManagerCopy == null) {
  6. reuseStrategyCopy = this.sslSocketFactory;
  7. if (reuseStrategyCopy == null) {
  8. if (this.sslContext != null) {
  9. reuseStrategyCopy = new SSLConnectionSocketFactory(this.sslContext, supportedProtocols, supportedCipherSuites, (HostnameVerifier)proxyAuthStrategyCopy);
  10. }
  11. }
  12. }
  13. }

可使用如下工具检测网关的SSL协议版本

SSL Server Test (Powered by Qualys SSL Labs)

参考

解决出现javax.net.ssl.SSLHandshakeException: PKIX path building failed 或 sun.security.validator.ValidatorException: PKIX path building failed的问题

HttpClient跳过SSL证书认证攻略_noophostnameverifier.instance-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/453642
推荐阅读
相关标签
  

闽ICP备14008679号