GeT*_*GeT 3 java security keystore jks osx-lion
我有一个密钥库,其密钥对由以下命令生成:
keytool -genkeypair -v -alias test-agent -keypass test-agent -storepass 123456.ABC -keystore test-agent.keystore -storetype JKS
Run Code Online (Sandbox Code Playgroud)
我填写所请求的证书信息,并正确生成密钥对的商店.
以下命令:
keytool -list -keystore test-agent.keystore -storepass 123456.ABC -storetype JKS
Run Code Online (Sandbox Code Playgroud)
返回:
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 1 entry
test-agent, Jul 13, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 7B:8F:D7:25:FF:34:D0:EF:44:87:46:E5:BF:18:C6:BF
Run Code Online (Sandbox Code Playgroud)
现在我将密钥库文件添加到我的构建路径并尝试使用在OSX Lion上运行的以下Java代码加载它:
public void loadKeyStore() {
try {
final Provider p = Security.getProvider("SUN");
final KeyStore keystore = KeyStore.getInstance("JKS",p);
final InputStream keyStoreInStream = this.getClass().getClassLoader().getResourceAsStream("test-agent.keystore");
if ( keyStoreInStream == null ) throw new RuntimeException("No keystore found!");
final char[] password = "123456.ABC".toCharArray();
try {
keystore.load(keyStoreInStream, password);
} catch (Exception e) {
log.error(String.format("Security library error! [%s]",e.getCause()),e);
}
} catch (KeyStoreException e) {
log.error("Can't initialize security library!",e);
}
}
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:633)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:38)
at java.security.KeyStore.load(KeyStore.java:1185)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用PKCS12(同时为keytool和代码设置,本例中的提供程序应该是SunJSSE),这会导致另一个异常:
java.io.IOException: DerInputStream.getLength(): lengthTag=111, too big.
at sun.security.util.DerInputStream.getLength(DerInputStream.java:544)
at sun.security.util.DerValue.init(DerValue.java:347)
at sun.security.util.DerValue.<init>(DerValue.java:303)
at com.sun.net.ssl.internal.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1200)
at java.security.KeyStore.load(KeyStore.java:1185)
Run Code Online (Sandbox Code Playgroud)
我不知道问题是什么.任何人都可以给我一个提示吗?
GeT*_*GeT 12
我找到了解决方案.实际上,我的项目部署存在问题.我使用maven,它的资源插件使用UTF8编码资源文件夹中的所有文件.此编码损坏了密钥库.解决方案是向pom文件添加ignore filter选项,并告诉maven不要对keystore文件进行编码.
jas*_*ase 10
将以下内容添加到pom.xml中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3110 次 |
最近记录: |