我创建了一个全局 JKS,其 keystore 密码为“changeme”。我使用 Keystore Explorer 创建了 JKS。
使用全局 JKS 背后的想法是应用程序可以从 S3 中拉取 JKS,然后使用自己的字符串密码重置 JKS。我们做了很多 SpringBoot API,并使用 JKS 来保护容器中的 Tomcat,以便我们可以连接 HTTPS。
但这就是我遇到的问题,当我更改 JKS keystore 密码时,我开始抛出 java.security.UnrecoverableKeyException: Cannot recovery key
错误。
在 keystore 资源管理器中,我没有为别名指定密码。当我进入 keystore 资源管理器更改别名密码时,它接受“changeme”作为密码。因此,我假设 keystore 资源管理器自动使用 Changeme 作为密码,因为我为 JKS keystore 密码提供了它。
诚然,我不是使用 JKS 和了解安全性复杂性的专家,但这让我难住了。
我还尝试使用以下命令通过 Keytool 更改 keystore 密码:
keytool -storepasswd -keystore myJKS.jks
和
keytool -keypasswd -alias myalias -keystore myJKS.jks
但是当我尝试更改别名时,我得到:
keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect
我做错了什么?
谢谢
最佳答案
您看到的错误是因为您可能在命令中提供了错误的keystore-password
。
基本了解 JKS
的原理和内容。 JKS(Java KeyStore)基本上是一个保护 key (对称 key )、 key 对(非对称 key )和证书的文件。它保护它们的方式是通过密码,该密码称为keystore-password
。而且 JKS 文件中的 key 也可以单独保护,这意味着它们可以有自己的密码,称为 key 密码
。
修改keystore密码的方法:
keytool -storepasswd -keystore [KEYSTORE] -storepass [OLD_KEYSTORE_PASSWORD] -new [NEW_KEYSTORE_PASSWORD]
修改 key 密码的方法:
keytool -keypasswd -keystore [KEYSTORE] -storepass [KEYSTORE_PASSWORD] -alias [ALIAS] -keypass [OLD_KEY_PASSWORD] -new [NEW_KEY_PASSWORD]
这些是与保护 spring-boot 应用程序相关的属性。您必须在这些属性中定义 keystore 密码和 key 密码。
server.ssl.ciphers= # Supported SSL ciphers.
server.ssl.client-auth= # Client authentication mode.
server.ssl.enabled=true # Whether to enable SSL support.
server.ssl.enabled-protocols= # Enabled SSL protocols.
server.ssl.key-alias= # Alias that identifies the key in the key store.
server.ssl.key-password= # Password used to access the key in the key store.
server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file).
server.ssl.key-store-password= # Password used to access the key store.
server.ssl.key-store-provider= # Provider for the key store.
server.ssl.key-store-type= # Type of the key store.
server.ssl.protocol=TLS # SSL protocol to use.
server.ssl.trust-store= # Trust store that holds SSL certificates.
server.ssl.trust-store-password= # Password used to access the trust store.
server.ssl.trust-store-provider= # Provider for the trust store.
server.ssl.trust-store-type= # Type of the trust store.
您可以在文档 here 中找到所有 spring-boot 属性.
如果您查看属性,就会发现有 server.ssl.key-store-password
和 server.ssl.key-password
。您可以要求用户在更改全局 JKS 密码后设置这两个值。
关于java - 如何更改 Java Keystore(JKS) keystore 和别名密码以便它们正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655545/