Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java密钥库-通过编程从密钥库文件中选择要使用的证书_Java_Ssl - Fatal编程技术网

Java密钥库-通过编程从密钥库文件中选择要使用的证书

Java密钥库-通过编程从密钥库文件中选择要使用的证书,java,ssl,Java,Ssl,我有一个java密钥库文件,其中包含多个客户端证书。我希望在Java应用程序中只选择其中一个证书来连接到服务。有没有一个简单的方法可以做到这一点?到目前为止,我找到解决方案的唯一方法是使用客户机证书详细信息(通过其别名找到)从原始密钥库文件在程序中创建一个新的密钥库。我认为有一种简单的方法可以说“使用keystore.jks文件中的证书并使用此别名”,而不必仅为要使用的证书创建新的密钥库。代码如下: // Set up Client Cert settings K

我有一个java密钥库文件,其中包含多个客户端证书。我希望在Java应用程序中只选择其中一个证书来连接到服务。有没有一个简单的方法可以做到这一点?到目前为止,我找到解决方案的唯一方法是使用客户机证书详细信息(通过其别名找到)从原始密钥库文件在程序中创建一个新的密钥库。我认为有一种简单的方法可以说“使用keystore.jks文件中的证书并使用此别名”,而不必仅为要使用的证书创建新的密钥库。代码如下:

        // Set up Client Cert settings
        KeyStore clientCertStore = KeyStore.getInstance("JKS");
        clientCertStore.load(new FileInputStream(clientKeystoreLocation), clientKeystorePassword);            

        // Create temporary one keystore, then extract the client cert using it's alias from keystore.jks, then create
        // a new keystore with this cert, that the process will use to connect with.
        KeyStore tempKstore = KeyStore.getInstance("JKS");
        tempKstore.load(null);
        tempKstore.setKeyEntry(certificateAlias, clientCertStore.getKey(certificateAlias, bwConfig.clientKeystorePassword),
                clientKeystorePassword, clientCertStore.getCertificateChain(certificateAlias));
        clientCertStore = tempKstore;

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(clientCertStore, clientKeystorePassword);            

        // Set up Truststore settings
        File truststoreFile = new File(TrustStoreLocation);
        KeyStore trustStore = KeyStore.getInstance("JKS");
        trustStore.load(new FileInputStream(truststoreFile), TrustStorePassword);
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        // Set to TLS 1.2 encryption
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        SSLSocketFactory ssf = sslContext.getSocketFactory();
        ssf.createSocket(serviceURL.getHost(), servicePort);

        bp.getRequestContext().put("com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory", ssf);

你的问题类似于

默认的
KeyManager
将选择握手时的第一个证书(根据服务器发送的CA列表),您可以构建自己的
X509KeyManager
来指定用于包装默认值的别名

final X509KeyManager origKm = (X509KeyManager)keyManagerFactory.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 ...
}
SSLContext

 sslContext.init(new KeyManager[] { km }, trustManagerFactory.getTrustManagers(), null);