代码之家  ›  专栏  ›  技术社区  ›  DilTeam

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

  •  0
  • DilTeam  · 技术社区  · 6 年前

    我有一个运行在ec2实例上的web服务器,它在内部调用使用spring boot构建的rest服务器。现在,我试图让这个rest服务器在ssl下运行。我到目前为止所做的是:

    1)使用此命令创建CSR&A密钥文件

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

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

    3)在我的EC2实例上成功安装了nginx下的证书。

    4)当我点击https下的主页时,它就工作了。我不再从浏览器收到“不安全”消息。

    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
    

    上一个命令给出一条错误消息: “密钥工具错误:java. Lang.Ext:输入不是X.509证书”

    但这是在步骤1中创建的一个文件,并且在NGNX下工作的文件是相同的。我遗漏了什么(除了我对设置ssls知之甚少!)?我需要第二个命令来指定application.properties中的“server.ssl.keylias”值。

    2 回复  |  直到 6 年前
        1
  •  2
  •   dave_thompson_085    6 年前

    不是一个真正的答案,而是溢于言表的评论。

    你不需要“生成”X.509证书;你已经从Godaddy那里得到了。如果(并且仅当)SpringBoot服务器以与(外部)nGixx相同的名称访问(我不清楚),则需要转换 一对私钥和证书链 从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

        2
  •  0
  •   DilTeam    6 年前

    谢谢你。下面两个命令完成了这个任务!

    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