2017-02-01 163 views
0

我的應用程序是在websphere上運行的Web服務提供者,並且SOAP Web服務客戶端由多個特定於每個客戶端的密鑰庫組成。該應用程序需要遷移到tomcat,我很震驚,因爲我需要使用不同的證書來建立基於傳入客戶端請求的後端服務器的TLS連接動態SSL密鑰庫/證書選擇

我正在使用springboot並有一種方法來配置密鑰庫和可信任。緊接着下面的鏈接:

http://zoltanaltfatter.com/2016/04/30/soap-over-https-with-client-certificate-authentication/

我想設置爲基於客戶端運行時的證書/密鑰庫。爲此,我連線密鑰庫和配置(客戶端)名稱,以便我可以動態使用客戶端特定的密鑰庫。但這是緊密耦合的,每當我有一個新的客戶端,我需要爲客戶端創建一個條目並設置相應的密鑰庫。

但是我想到了另一種方法,比如我將所有證書都保存在一個密鑰庫中,我們如何動態訪問客戶端特定的證書?

回答

0

您可以使用密鑰庫來管理您的所有客戶端證書,這些證書將通過別名進行標識。

默認KeyManager將在握手中選擇第一個證書,因此在創建連接之前無需構建自己的X509KeyManager來指定要使用的別名。見How I can tell alias of the wanted key-entry to SSLSocket before connecting?

KeyStore keystore = ... //The keystore with all your certificates 

//The keymanager for an specific connection 
KeyManagerFactory kmf= KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
kmf.init(keystore, password.toCharArray()); 

//Create a keyManager wrapper that returns the alias to use 
final X509KeyManager origKm = (X509KeyManager)kmf.getKeyManagers()[0]; 
X509KeyManager km = new X509KeyManager() { 
    public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { 
     return "alias"; 
    } 

    public X509Certificate[] getCertificateChain(String alias) { 
     return origKm.getCertificateChain(alias); 
    } 

// override the rest of the methods delegating to origKm ... 
} 

HttpsUrlConnectionMessageSender

HttpsUrlConnectionMessageSender messageSender = new HttpsUrlConnectionMessageSender(); 
//messageSender.setKeyManagers(keyManagerFactory.getKeyManagers()); 
messageSender.setKeyManagers(new KeyManager[] { km }); 
+0

我想你的方式注入新keyManager,它的工作,但我面臨的另一個問題。我聲明瞭X509Keymanager並重寫了上述兩種方法,但是當我打電話給目標服務器時,我得到了下面的錯誤信息'main,RECV TLSv1.2 ALERT:fatal,unexpected_message main,處理異常:javax.net.ssl .SSLException:收到致命錯誤'我通過將KeyManager對象提供給HttpsUrlConnectionMessageSender而不是X509KeyManager來修復此錯誤。服務器SSL跟蹤顯示服務器未從客戶端獲取_CERTIFICATE-VERIFY message_,因此連接突然終止。 – user3709612

+0

確切的錯誤是:'引起:org.springframework.ws.client.WebServiceIOException:I/O錯誤:收到致命警報:unexpected_message;嵌套異常是javax.net.ssl.SSLException:收到致命警報:unexpected_message \t at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:561)〜[spring-ws-core-2.3.1 .RELEASE.jar:2.3.1.RELEASE]引起:javax.net.ssl.SSLException:收到致命警報:unexpected_message \t at sun.security.ssl.Alerts.getSSLException(Alerts.java:208 )〜[na:1.8.0_71]' – user3709612

+0

我解決了這個問題,看起來像我的應用程序獲取私鑰爲空,因爲我沒有重寫該方法。這是修復:'@Override \t \t \t公共專用密鑰getPrivateKey(字符串爲arg0){ \t \t \t \t // TODO自動生成方法存根 \t \t \t \t返回origKm.getPrivateKey(爲arg0); \t \t \t}' – user3709612