2016-11-27 118 views
0

我想連接到https服務器,該服務器有self signed certificate,但不修改我的客戶端代碼。告訴java客戶端代碼接受自簽名證書

我知道這個問題已被問了很多次,但我無法得到它的工作。下面是我做了什麼:

  1. 在Firefox中打開https服務器

  2. 點擊上面的URL信息,那麼更多的信息,然後查看證書,則詳細信息,然後單擊導出。

  3. 選擇默認導出類型設置(X.509 Certificate (PEM),保存爲certificate.crt),並將其保存到磁盤。

  4. 打開命令提示符,轉到客戶端代碼,這在我的情況是c:\jdk-7u55-windows-x64\jre\bin

  5. 輸入以下命令使用的java_home文件夾:

    keytool -import -v -trustcacerts 
          -alias server-alias -file C:\Downloads\certificate.crt 
          -keystore cacerts.jks -keypass changeit 
          -storepass changeit 
    

我也嘗試過:

keytool -import -v -trustcacerts 
        -alias server-alias2 -file C:\Downloads\certificate.crt 
        -keystore keystore.jks 
  1. 重啓客戶端應用程序

這一切之後,我仍然獲得了SSLHandshakeException(無法找到有效的認證路徑請求的目標)。 全部例外日誌:

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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 

我不知道我做錯了什麼,還能做什麼或如何進一步調試。

+0

您可以驗證證書是否已正確導入信任存儲區。使用以下命令列出信任存儲中的證書。 'keytool -list -keystore cacerts.jks' 也請追加與失敗相關的日誌。 – dammina

+0

@dammina是的,列表命令顯示服務器別名是今天導入的。日誌只顯示SSLHandshakeException。我已添加完整的例外 – PeteSpo

+0

http://stackoverflow.com/questions/9210514/unable-to-find-valid-certification-path-to-requested-target-error-even-after-c – dammina

回答

0

指定所需的絕對路徑cacerts.jks應該有所幫助。 keytool的當前使用只是創建新的.jks到您當前的目錄(我想這是)c:\jdk-7u55-windows-x64\jre\bin。默認的cacerts位置通常在JAVA_HOME/jre/lib/security/cacerts。或者,您可以使用 -Djavax.net.ssl.trustStore=<path to custom store> -Djavax.net.ssl.trustStorePassword=<custom store_passwd>作爲您的應用的參數。 More info

相關問題