0
我試圖將HTTP請求發送到使用SSL進行保護的應用程序服務器。我有一個來自服務器團隊的自簽名X.509證書。我跟着從documentation教程,但還是得到了SSLHandshakeException
:java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
無法使用自簽名證書實現到服務器的SSL連接
我使用這個代碼:
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
AssetManager assManager = ShoppingHelperApp.getContext().getAssets();
InputStream is = assManager.open("keystore2.crt");
InputStream caInput = new BufferedInputStream(is);
Certificate ca = cf.generateCertificate(caInput);
caInput.close();
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
context = SSLContext.getInstance("TLS");
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(context.getSocketFactory());
有誰有線索如何解決呢?
感謝您的迴應!在我看來,除了證書的擴展名外,我們的片段幾乎相同。我試着用.pem,但它也沒有奏效:( – mol
@mol它不是什麼大問題;)http://stackoverflow.com/questions/4691699/how-to-convert-crt-to-pem – mklimek
是的,我已經嘗試過了,同樣的例外。 – mol