在我的spring MVC java应用程序中,我有一个方法:
public static PrivateKey getPrivateKey( String password, InputStream privateKeyFileStream) {
KeyStore ks;
Key key = null;
try {
ks = KeyStore.getInstance("PKCS12");
ks.load(privateKeyFileStream, password.toCharArray());
Enumeration<String> enumeration = ks.aliases();
// uses the default alias
String keyAlias = (String) enumeration.nextElement();
key = ks.getKey(keyAlias, password.toCharArray());
} catch (KeyStoreException e) {
ErrorLog.Log(ErrorLog.FATAL, "Error creating WebService. Failed to read private key", e, "WebService", "constructor");
} catch (NoSuchAlgorithmException e) {
ErrorLog.Log(ErrorLog.FATAL, "Error creating WebService. Failed to read private key", e, "WebService", "constructor");
} catch (CertificateException e) {
ErrorLog.Log(ErrorLog.FATAL, "Error creating WebService. Failed to read private key", e, "WebService", "constructor");
} catch (IOException e) {
ErrorLog.Log(ErrorLog.FATAL, "Error creating WebService. Failed to read private key", e, "WebService", "constructor");
} catch (UnrecoverableKeyException e) {
ErrorLog.Log(ErrorLog.FATAL, "Error creating WebService. Failed to read private key", e, "WebService", "constructor");
}
return (PrivateKey) key;
}
当我在本地应用程序中使用此方法时,如下所示:
File file = new File("../bin/file.p12");
InputStream privateKeyFileStream = null;
try {
privateKeyFileStream = FileUtils.openInputStream(file);
} catch (IOException e) {
ErrorLog.Log(ErrorLog.FATAL, "Error creating WebService.", e, "WebService", "constructor");
}
PrivateKey privateKey = getPrivateKey("password", privateKeyFileStream);
一切正常。 p12文件位于我的tomcat文件夹的bin目录中。
但是,对于我的测试环境,我将相同的p12文件放在tomcat目录中的同一个bin文件夹中,当尝试读取该文件时,我得到以下异常:
java.util.NoSuchElementException
at java.util.Collections$EmptyEnumeration.nextElement(Collections.java:3083)
at com.class.util.class.Class.getPrivateKey(ClassUtils.java:92)
at com.class.util.class.Class.getInstance(Class.java:77)
at
抛出错误的代码行是:
String keyAlias = (String) enumeration.nextElement();
它基本上无法在p12文件上找到任何证书条目,但是当我运行
时keytool -list -keystore file.p12 -storepass password -storetype PKCS12 -v
在我的本地系统和测试环境位置,它显示:
Your keystore contains 1 entry
Alias name: test
Creation date: Feb 11, 2015
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
出现什么问题?
答案 0 :(得分:0)
我决定从应用程序的资源目录中读取p12文件,该文件工作正常。