2012-04-02 177 views
1

我寫了一個模塊,通過https連接到服務與身份驗證。設置正確的路徑到密鑰存儲後,它工作正常。當我想在Tomcat應用程序中使用該模塊(作爲jar)時出現問題。我設置適當的路徑(絕對路徑)密鑰存儲以及但是當我嘗試連接,我得到的握手例外SSLHandshakeException當多個ssl連接

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 

我記得我,當我有不正確的密鑰庫之前,此消息。我需要做更多的事情才能使它在Tomcat下工作。我錯過了其他任何問題? 我通過https連接到另一個服務,無需身份驗證,這工作正常(在Tomcat應用程序中)。

編輯:問題是運行一個項目,通過ssl連接到不同的服務(不僅在Tomcat中)。第一個沒有認證。所以我編輯了標題

+0

這是錯誤在客戶端,還是在您的Tomcat日誌?當客戶端信任庫中沒有服務器證書(或其中一個父證書)時,通常會出現此錯誤 – 2012-04-02 13:30:57

+0

您在何處以及如何配置信任庫? – Bruno 2012-04-02 13:38:42

+0

你的答案似乎合適,你應該接受它(所以這個問題被標記爲已解決) – 2012-04-13 08:43:16

回答

1

我再一次給自己答案。 看完How to control SSLContext scope後,我決定做同樣的事情,它工作正常。默認情況下,我沒有設置任何的trustStore(所以默認的cacerts使用),當我需要驗證我用

httpsUrlConnection.setSSLSocketFactory(getSSLContext().getSocketFactory()); 

時getSSLContext()返回我寫上面(不用setDefault)

我會想知道如何更改Tomcat應用程序中的默認SSLContext,所以如果有人可以幫助,我會很感激

+0

這不是一個論壇,而是一個問答網站。試着最多隻有一個問題的答案。如果您嘗試了一些新功能,請編輯您的問題,而不是在自己的問題的答案中添加更多詳細信息。 – Bruno 2012-04-03 12:38:51

+0

對不起,我會記住的 – jasiustasiu 2012-04-04 08:57:41

1

Setting multiple truststore on the same JVM給了我一個答案。我只需要設置我的關鍵工廠以及信任工廠,它的工作原理:)

System.setProperty不設置已設置的ssl屬性。

// load your key store as a stream and initialize a KeyStore 
    InputStream trustStream = new FileInputStream("Resources/keystore.ImportKey"); 
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 

    // if your store is password protected then declare it (it can be null however) 
    String trustPassword = "changeit"; 

    // load the stream to your store 
    trustStore.load(trustStream, trustPassword.toCharArray()); 

    // initialize a trust manager factory with the trusted store 
    TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
    trustFactory.init(trustStore); 

    KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
    keyFactory.init(trustStore, trustPassword.toCharArray()); 

    // get the trust managers from the factory 
    TrustManager[] trustManagers = trustFactory.getTrustManagers(); 
    KeyManager[] keyManagers = keyFactory.getKeyManagers(); 

    // initialize an ssl context to use these managers and set as default 
    SSLContext sslContext = SSLContext.getInstance("SSL"); 
    sslContext.init(keyManagers, trustManagers, null); 
    SSLContext.setDefault(sslContext); 

工作正常!