Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed Error?

后端 未结 22 2469
名媛妹妹
名媛妹妹 2020-11-21 05:59

Edit :- Tried to format the question and accepted answer in more presentable way at mine Blog

Here is the original issue.

22条回答
  •  粉色の甜心
    2020-11-21 06:29

    add this to your code :

    TrustManager[] trustAllCerts = new TrustManager[]{
               new X509TrustManager() {
                   @Override
                   public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                       return new X509Certificate[0];
                   }
    
                   @Override
                   public void checkClientTrusted(
                           java.security.cert.X509Certificate[] certs, String authType) {
                   }
    
                   @Override
                   public void checkServerTrusted(
                           java.security.cert.X509Certificate[] certs, String authType) {
                   }
               }
           };
    
           try {
               SSLContext sc = SSLContext.getInstance("SSL");
               sc.init(null, trustAllCerts, new java.security.SecureRandom());
               HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
           } catch (GeneralSecurityException e) {
           }
    

提交回复
热议问题