如何从 openssl 生成的密钥生成 X.509 证书

How do I generate X.509 certificate from key generated by openssl

我在 ec2 实例上有一个 Web 服务器 运行,它在内部调用一个使用 Spring Boot 构建的 REST 服务器。现在,我正尝试在 SSL 下获取此 REST 服务器 运行。这是我到目前为止所做的:

1) 使用此命令创建了 CSR 和密钥文件

openssl req -newkey rsa:2048 -nodes -keyout mydomain.key -out mydomain.csr

2) Copied 'csr' to get SSL certificate from GoDaddy.

3) 在我的 ec2-instance 上成功安装 Nginx 下的证书。

4) 当我在 https 下访问主页时,它起作用了。我不再从浏览器收到 'Not secure' 消息。

5) 登录失败,因为它进行了 REST 调用,但 REST 服务器不在 SSL 下 运行 所以我试图在 SSL 下获取它 运行。

6) 运行 以下命令:

keytool -import -alias mydomain -keystore tomcat.keystore -trustcacerts -file mydomain.com.chained.crt
keytool -import -alias mydomain-key -keystore tomcat.keystore -trustcacerts -file mydomain.key

前面的命令给我一条错误信息: "keytool error: java.lang.Exception: Input not an X.509 certificate"

但这是在上面的第 1 步中创建的文件,同样的文件在 Nginx 下工作。我错过了什么(除了我对设置 SSL 知之甚少!)?我需要第二个命令来指定 application.properties 中 'server.ssl.keyAlias' 的值,我相信。

不是真正的答案,而是满溢的评论。

您不需要 'generate' X.509 证书;您已经从 GoDaddy 获得了它。如果(且仅当)使用与(外部)nginx 相同的名称访问 SpringBoot 服务器时——我不清楚——你需要转换 对私钥和证书链 从 PEM 格式到 Java 使用的格式。参见:
How to import an existing x509 certificate and private key in Java keystore to use in SSL?
How can I set up a letsencrypt SSL certificate and use it in a Spring Boot application?
How to use .key and .crt file in java that generated by openssl?
Importing the private-key/public-certificate pair in the Java KeyStore
也许 Import key and SSL Certificate into java keystore

谢谢@Dave_thompson_085。以下 2 个命令就成功了!

openssl pkcs12 -export -in mydomain.com.chained.crt -inkey mydomain.key -out keystore.p12 -name my-alias -caname root

keytool -importkeystore -deststorepass mypassword -destkeypass mypassword -destkeystore keystore.jks -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass mypassword -alias my-alias

然后在 application.properties 中我指定了以下属性:

server.port=8443
server.ssl.enabled=true
security.require-ssl=true
server.ssl.key-store=/etc/nginx/ssl/keystore.jks
server.ssl.key-store-password=mypassword
server.ssl.keyStoreType=JKS
server.ssl.keyAlias=my-alias