【发布时间】:2015-08-04 14:27:57
【问题描述】:
由于某种原因,我在单元测试中加载的 KeyStore 似乎是空的,我不知道为什么。
我在这里有一个带有证书的密钥库文件:src/test/resources/public-keystore-name
所以,在终端中运行这个命令:
../src/test/resources$ keytool -list -keystore public-keystore-name -storetype PKCS12
我得到以下输出:
Keystore type: PKCS12
Keystore provider: SunJSSE
Your keystore contains 1 entry
aliasname, May 22, 2015, trustedCertEntry,
Certificate fingerprint (SHA1): 4E:87:CF:EF:FC:E1:37:63:36:E0:26:0C:1E:B3:65:BB:48:3A:83:1A
在我的单元测试中,我可以从这个文件加载并启动一个 KeyStore,但是我无法获取我存储在其中的证书。证书有别名“aliasname”,密码“password”。
@Test
public void testUtil() throws Exception {
KeyStore publicKS = KeyStore.getInstance("PKCS12");
File publicKeyStoreFile = FileUtils.getFile("src/test/resources/public-keystore-name");
FileInputStream fisPublic = new FileInputStream(publicKeyStoreFile);
publicKS.load(fisPublic, "password".toCharArray());
Certificate cert = publicKS.getCertificate("aliasname");
System.out.println("Cert is: " + cert);
}
将始终打印: “证书为:空”
为什么这个单元测试中的 KeyStore 是空的? (使用 Keystore.aliases() 查找别名将返回一个空集)。
【问题讨论】:
标签: java unit-testing certificate keystore