调用 Https服务报 SunCertPathBuilderException: unable to find valid certification path to requested target

报错报文:

java.lang.Exception: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

问题原因:Java自带的安全证书不受信任,在代码里面的实现时忽略证书,绕开SSL验证

解决代码如下:

public static void ignoreSsl() throws Exception {
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    trustAllHttpsCertificates();
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
public 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 void checkServerTrusted(X509Certificate[] certs, String authType)
            throws CertificateException {
        return;
    }

    public void checkClientTrusted(X509Certificate[] certs, String authType)
            throws CertificateException {
        return;
    }
}
  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GRADLE SunCertPathBuilderException: unable to find valid certification path to requested target错误是由于无法找到有效的证书路径引起的。 这个错误通常发生在使用HTTPS连接时,因为Java无法验证服务器的证书。解决这个问题的常用方法是下载站点证书,并将其导入到密钥库中,然后通过-Djavax.net.ssl.trustStore=... JVM选项来使用该密钥库。 另外,您还可以在Gradle的构建配置文件中配置信任的证书,以避免出现证书验证错误。 请注意,这个错误可能是由于服务器配置问题或证书过期等原因引起的,您可能需要与服务器管理员联系以获得更多支持。<span class=&quot;em&quot;>1</span><span class=&quot;em&quot;>2</span><span class=&quot;em&quot;>3</span> #### 引用[.reference_title] - *1* *2* [SunCertPathBuilderException: unable to find valid certification path to requested target错误的方法](https://blog.csdn.net/wh445306/article/details/128265595)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] - *3* [gradle-trust-all:一个用于禁用 SSL 证书验证的 gradle 插件](https://download.csdn.net/download/weixin_42120541/19325493)[target=&quot;_blank&quot; data-report-click={&quot;spm&quot;:&quot;1018.2226.3001.9630&quot;,&quot;extra&quot;:{&quot;utm_source&quot;:&quot;vip_chatgpt_common_search_pc_result&quot;,&quot;utm_medium&quot;:&quot;distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2&quot;}}] [.reference_item style=&quot;max-width: 50%&quot;] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值