前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >http请求的方法里怎么设置信任所有ssl证书?(PKIX path building failed)

http请求的方法里怎么设置信任所有ssl证书?(PKIX path building failed)

作者头像
凯哥Java
发布2019-06-28 17:39:13
1.8K0
发布2019-06-28 17:39:13
举报
文章被收录于专栏:凯哥Java凯哥Java凯哥Java

工作中,有时候需要跨系统调用。这个时候HttpURLConnection,而现在很多网站都是用的是HTTPS。我们知道HTTPS都是有证书的。证书有的是花钱买的,有的没有花钱。这请情况下,有时候,有些https请求,就不是可信任的。

错误信息:

de4f1aac0be08bd5abe11061f5957f16.png
de4f1aac0be08bd5abe11061f5957f16.png

解决方案有两种:

解决方案

方案一:

按照要求,把证书上传到服务器上,具体方法自行百度解决,这里不做叙述。

方案二:

添加下面的代码和工具类,工具类在下面附件中有,直接下载即可。

//省略代码。。。realUrl = new URL(url);if("https".equalsIgnoreCase(realUrl.getProtocol())){
   SslUtils.ignoreSsl();
}//省略代码。。。

SslUtils工具类:

package com.thinkgem.jeesite.modules.tbk.uitl;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
 * java 信任SSL证书
 * @author 凯哥Java
 * @website www.kaigejava.com
 *
 */
public class SslUtils {
 
    private static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }
 
    static class miTM implements TrustManager,X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
 
        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }
 
        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }
 
        public void checkServerTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
 
        public void checkClientTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
    }
     
    /**
     * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
     * @throws Exception
     */
    public static void ignoreSsl() throws Exception{
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解决方案
    • 方案一:
      • 方案二:
      相关产品与服务
      SSL 证书
      腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档