使用Java,我必须从带有p12证书的URL读取JSON字符串。我已使用
成功将证书导入jre1.8.0_51 \ lib \ security \ cacertskeytool -importkeystore -deststorepass changeit -destkeystore cacerts -srckeystore ..\certificate\cert1.p12 -srcstoretype pkcs12 -srcstorepass mypass -alias cert1
我还可以在Chrome中看到JSON字符串。但是,当我尝试从Java中读取它时,我得到:
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
我正在使用此代码:
URL url = new URL("https://myurl");
KeyStore ks = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream("certificate/cert1.p12");
ks.load(fis, "mypass".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, "mypass".toCharArray());
SSLContext sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory sslSF = sslCtx.getSocketFactory();
URLConnection conn = url.openConnection();
if(conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(sslSF);
}
conn.connect();
但是,此代码会产生obove异常。我在Windows 10上。我错过了什么?谢谢!