Java keytool 从 url/port 添加服务器证书的简单方法
我有一个带有自签名证书的服务器,但也需要客户端证书身份验证。我在尝试获取原始 CA 服务器证书时遇到了困难,因此我可以将其导入密钥库。有人对如何轻松做到这一点有一些建议吗?
I have a server with a self-signed certificate, but also requires client side cert authentication. I am having a rough time trying to get the raw CA server cert so I can import it into a keystore. Anyone have some suggestions on how to easily do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正在研究如何在使用 jenkins cli 时信任证书,并发现
https://issues.jenkins-ci.org/browse/JENKINS-12629 其中有一些秘诀那。
这将为您提供证书:
如果您只对证书部分感兴趣,请通过管道将其剪切到:
并重定向到文件:
然后使用 keytool 导入它:
一次性:
Was looking at how to trust a certificate while using jenkins cli, and found
https://issues.jenkins-ci.org/browse/JENKINS-12629 which has some recipe for that.
This will give you the certificate:
if you are interested only in the certificate part, cut it out by piping it to:
and redirect to a file:
Then import it using keytool:
In one go:
我使用 openssl,但如果您不愿意,或者您所在的系统(特别是 Windows)没有它,自 2011 年的 java 7
keytool
可以完成整个工作:相反,对于 java 9 及以上版本,以及在许多情况下的早期版本,Java 可以使用 PKCS12 文件作为密钥库,而不是传统的 JKS 文件,并且OpenSSL 可以创建一个PKCS12 无需 keytool 的任何帮助:但是(显然我在 2018 年还没有发现)这不能作为“标准”(Oracle/OpenJDK)Java 加密的信任库因为这需要 PKCS12 中的受信任证书具有 OpenSSL 不知道的特殊属性。如果您安装并使用 BouncyCastle 提供程序并指定可能影响(可能会破坏)其他事物的存储类型,并且使用
-caname确实有效代码>. (更正:
-caname
不是-name
或nm
)I use openssl, but if you prefer not to, or are on a system (particularly Windows) that doesn't have it, since java 7 in 2011
keytool
can do the whole job:Conversely, for java 9 up always, and for earlier versions in many cases, Java can use a PKCS12 file for a keystore instead of the traditional JKS file, andOpenSSL can create a PKCS12 without any assistance from keytool:BUT (as I apparently hadn't yet found in 2018) this won't work as a truststore with 'standard' (Oracle/OpenJDK) Java crypto because that requires trusted cert(s) in a PKCS12 to have a special attribute OpenSSL doesn't know about. This does work if you install and use the BouncyCastle provider and specify the storetype which can affect (maybe break) other things, AND you specify 'friendly' name(s) with
-caname
. (corrected:-caname
not-name
ornm
)我发现有几种方法可以做到这一点:
There were a few ways I found to do this:
您可以使用 Firefox、此网站导出证书有说明。然后使用 keytool 添加证书。
You can export a certificate using Firefox, this site has instructions. Then you use keytool to add the certificate.
只需将 dnozay 的答案暴露给函数,以便我们可以同时导入多个证书。
将其保存到 .sh 文件然后运行它。
Just expose dnozay's answer to a function so that we can import multiple certificates at the same time.
Save it to a .sh file then run it.