问题描述:
Caused by: java.io.IOException: Invalid keystore format
问题分析:
1、使用KeyStore.load()方法merlion.jks文件不对导致报错。
/**
* 获取 KeyPair
* 生成 keypair 可按下述方法
* keytool -genkey -alias merlion -keyalg RSA -storepass admin@123 -keysize 1024 -keystore merlion.jks -validity 3655
*
* @return KeyPair
*/
@Bean
public KeyPair keyPair() {
try (InputStream inputStream = new ClassPathResource("keystore/merlion.jks").getInputStream()) {
KeyStore keyStore = KeyStore.getInstance("jks");
String alias = "merlion";
char[] password = "admin@123".toCharArray();
keyStore.load(inputStream, password);
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password);
PublicKey publicKey = keyStore.getCertificate(alias).getPublicKey();
return new KeyPair(publicKey, privateKey);
} catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException e) {
throw new RuntimeException(e);
}
}
解决办法:
打开jdk的bin目录,进入cmd窗口,执行keytool -genkey -alias merlion -keyalg RSA -storepass admin@123 -keysize 1024 -keystore merlion.jks -validity 3655命令,重新生成merlion.jks文件。再把生成的merlion.jks文件替换项目中原有的merlion.jks文件。