0

我正在尝试通过 java 代码连接到 HTTPS 服务器以进行测试。我并不担心安全性。我添加了 trustmanager 来接受所有证书。如下

 TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
        } };
        try {
            System.out.println("inside Try2");
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        }
        catch (Exception e) {
            System.out.println("Exception11");
        }

即使按照许多论坛上的建议添加此代码,我也会收到 SSL 握手异常。异常跟踪:

[testng] Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
   [testng]     at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
   [testng]     at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
   [testng]     at sun.security.validator.Validator.validate(Validator.java:260)
   [testng]     at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
   [testng]     at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
   [testng]     at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
   [testng]     at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
   [testng]     ... 56 more
   [testng] Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
   [testng]     at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
   [testng]     at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
   [testng]     at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
   [testng]     ... 62 more
4

0 回答 0