2013-11-02 251 views
0

我試圖從需要使用用戶名/密碼進行身份驗證的網站下載xml文件。通過手動瀏覽當然沒有問題,彈出窗口需要插入用戶/密碼,但在這裏我希望它自動完成。這是我的代碼HTTPS身份驗證錯誤

Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "mypassword".toCharArray()); } });  
try { 
    URL testURL=new URL("testingURL"); 
    try { 
    FileUtils.copyURLToFile(testURL, dest); 
} catch (IOException e) { 
     e.printStackTrace(); 
     } 
} catch (MalformedURLException e) { 
     e.printStackTrace(); 
} 

,但是我得到的異常

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 
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886) 
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276) 
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270) 
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341) 
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153) 
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868) 
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804) 
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016) 
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) 
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) 
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) 
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:515) 
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299) 
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 
at java.net.URL.openStream(URL.java:1037) 
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1460) 
at xmlModifier.main(xmlModifier.java:146) 
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385) 
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) 
at sun.security.validator.Validator.validate(Validator.java:260) 
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326) 
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231) 
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126) 
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323) 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196) 
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268) 
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380) 

有人能幫忙嗎?

+0

你是什麼意思的框架?我只是想實現在http://stackoverflow.com/questions/955624/download-a-file-from-the-internet-using-java-how-to-authenticate中給出的答案,以便下載一個文件... – splinter123

回答

0

服務器使用服務器端SSL證書。您可以下載證書並將其安裝到您的本地信任庫或修補SSLContext以接受任何證書(谷歌的詳細信息,我已經在幾年前完成)。與客戶端證書相反,服務器證書使客戶端(即您)可以驗證服務器的唯一性,因此可以「始終信任」。