Java - 获取 KeyStore 中刚刚加载的 Entry 的别名

问题描述

我正在尝试使用 .pfx 证书对 XML 进行签名,签名在只有一个证书的测试环境中工作正常,但在生产中,我的系统将存储许多 .pfx 证书。如何获取刚刚使用 load() 方法加载的证书的别名?

...
// Load the KeyStore and get the signing key and certificate.
KeyStore ks = KeyStore.getInstance("PKCS12");
// certPath contain the path for the .pfx file
ks.load(new FileInputStream(certPath),password.tochararray()); // How to get the alias for this certificate that was just loaded
String alias = ks.aliases().nextElement(); // This works only if the keystore has one just one loaded entry
KeyStore.PrivateKeyEntry keyEntry = 
        (KeyStore.PrivateKeyEntry) ks.getEntry(alias,new KeyStore.PasswordProtection(password.tochararray()));
X509Certificate cert = (X509Certificate) keyEntry.getCertificate(); // Get the just added certificate
...

如果我做错了其他事情并且存在使用 .pfx 对 XML 进行签名的更简单方法,请告诉我。

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...