我的应用程序是一个在 websphere 上运行的网络服务提供者,一个 SOAP 网络服务客户端由几个特定于每个客户端的 keystore 组成。该应用程序需要迁移到 tomcat,我很震惊,因为我需要使用不同的证书根据传入的客户端请求与后端服务器建立 TLS 连接
我正在使用 springboot 并且可以配置 keystore 和信任库。点击以下链接:
http://zoltanaltfatter.com/2016/04/30/soap-over-https-with-client-certificate-authentication/
我想在运行时根据客户端设置证书/ keystore 。为此,我连接了 keystore 和配置(客户端)名称,以便我可以动态使用客户端特定的 keystore 。但这是紧耦合的,每次我有一个新客户端,我都需要为客户端创建一个条目并设置相应的 keystore 。
但我想到了另一种方法,比如说我将所有证书保存在一个 keystore 中,我们如何才能动态访问客户端特定的证书?
最佳答案
您可以使用 keystore 来管理您所有的客户端证书,这些证书将由别名标识。
默认的 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
中注入(inject)新的 keyManager
HttpsUrlConnectionMessageSender messageSender = new HttpsUrlConnectionMessageSender();
//messageSender.setKeyManagers(keyManagerFactory.getKeyManagers());
messageSender.setKeyManagers(new KeyManager[] { km });
关于java - 动态 SSL keystore /证书选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41986959/