如何为 2 个不同的域存储 KeyStore

How to store KeyStore for 2 different domains

我想以编程方式存储 2 个不同域的密钥库。下面是为域 A 加载密钥库的代码。我想为域 B 执行此操作。两个密钥库将在同一个应用程序中使用。

public static SSLContext createSSLContext() throws Exception{
    KeyStore clientStore = createKeyStore();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(clientStore, "password".toCharArray());
    KeyManager[] kms = kmf.getKeyManagers();
    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(kms, null, new SecureRandom());

    return sslContext;
}
public static KeyStore createKeyStore() throws Exception{
    KeyStore clientStore = KeyStore.getInstance("PKCS12");
    try {
        clientStore.load(new ByteArrayInputStream("PKCS12 info"), "password".toCharArray());

    } catch(Exception e){
        e.printStackTrace();
    }
    return clientStore;
}

如 dave-thompson-085 所述,我缺少 TrustStore 密钥。来自 post 的片段很有帮助。 Programmatically Import CA trust cert into existing keystore file without using keytool