2015-04-01 50 views
0

我們的遺留應用程序在開發環境中有自簽名證書。應用程序可以從瀏覽器訪問(具有證書錯誤 - 該CA根證書不受信任,因爲它不在受信任的根證書頒發機構存儲中),但是從Java代碼(使用HttpsURLConnection)訪問應用程序時,會給出SSLHandshakeException。從Java代碼連接到HTTPS時出現SSLHandshakeException

經過許多計算器問題(包括telling java to accept self-signed ssl certificate)和一些互聯網上提供的文章去後,我也跟着下面的步驟,但沒有任何運氣尚未:

    從瀏覽器
  1. 導出的證書(mycert.cer)
  2. 進口這mycert.cer在JVM信任使用命令: 的keytool -import -alias -file對myApp -keystore mycert.cer的cacerts -storepass的changeit

運行之後,檢查是否有人SUC cessfully使用 的keytool -list -keystore cacerts的

添加即使從Java代碼中添加此證書在java中信任,HTTPS後給我 javax.net.ssl.SSLHandshakeException:收到致命警報:handshake_failure

上任何指針這是非常感謝。

請注意:這個自簽名證書是很久以前創建的,我們沒有太多關於它是如何創建的細節。

更多的細節: Java版本 - Java 1.6的 完整的錯誤跟蹤:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) 
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1822) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1004) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1215) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1199) 
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195) 
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318) 

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1514) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1508) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1162) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234) 

Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) 
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1822) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1004) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1215) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1199) 
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195) 
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318) 

    ... 1 more 
+0

握手異常可以是任何東西,比如協議版本,密碼不匹配......請提供調試輸出,數據包捕獲,大約使用等 – 2015-04-01 05:58:48

+0

斯特芬喜Java版本的信息,我是新來的SSL,請你可以讓我知道如何找到數據包捕獲? Java版本是1.6 – nshweta 2015-04-01 06:24:23

回答

1

這本來是更好,如果你發佈出現SSLHandshakeException堆棧跟蹤。但是,如果你得到了一些這樣的事:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX路徑建設失敗: sun.security.provider.certpath.SunCertPathBuilderException:無法通過 找到有效的認證路徑以請求目標

然後請看看下面的URL。這些將是有用的:

a。 http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/

b。 http://code.google.com/p/java-use-examples/source/browse/trunk/src/com/aw/ad/util/InstallCert.java

如果你得到了一些這樣的事:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException:沒有主題備用名稱 目前

使用-ext san=ip:<IP Address>,san(主題備用名稱),同時創建密鑰庫。這從JDK 1.7開始支持。

+0

感謝索姆納特共享的細節。我的客戶端應用程序(消耗我的應用程序的API)報告 - javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路徑建設失敗。當本地我試圖創建一個Java應用程序訪問我的HTTPS URL重現此,我得到「javax.net.ssl.SSLHandshakeException:收到致命警報:handshake_failure」(在我的問題跟蹤)。我試圖按照你給mkyonk例子的步驟,但得到的錯誤 - 「無法獲得服務器證書鏈」 – nshweta 2015-04-01 07:00:36

0

最好的辦法是抓住wireshark並驗證SSL警報值並從那裏開始。這可能是一個警報值40,這意味着您沒有找到兼容的協議。服務器要求客戶端不支持的SSL/TLS版本,或者它們沒有兼容的密碼套件。

+0

您還可以啓用SSL調試Java中javax.net.debug =所有。 – gobbly 2015-04-11 06:07:31

相關問題