【问题标题】:How to enable HTTPS using Goddady SSL in a Spring Boot application, deployed as WAR file in Tomcat如何在 Spring Boot 应用程序中使用 Goddady SSL 启用 HTTPS,在 Tomcat 中部署为 WAR 文件
【发布时间】:2023-02-01 22:09:06
【问题描述】:

我有一个 Spring Boot 应用程序,作为 WAR 文件部署在 Godaddy 的 Tomcat 中。现在,我确实需要在其上启用 HTTPS。我已经在我们的域上安装了 SSL,并且 https 正在处理它。DNS 和 SSL 证书由 GoDaddy 管理.我已经下载了 tomcat 的证书,其中包含“randomhex.crt”、“randomhex.pem”、“gd_bundle-g2-g1.crt”、“gdig2.crt.pem”等文件,然后我生成了“keystore.jks” &'keystore.p12' 按照以下命令使用这些文件。

第1步:

"keytool -import -trustcacerts -alias intermediate -file gd_bundle-g2-g1.crt -keystore keystore.jks"  using password as 'password1'

第2步:

"keytool -import -trustcacerts -alias 'alias1' -file e1......7.crt -keystore keystore.jks"  using password as 'password1'

第 3 步:

"keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -srcstoretype JKS -deststoretype PKCS12 -deststorepass 'password2' -srcalias 'alias1' -destalias 'alias2'"   using password as 'password1'

我现在知道我需要将这个 'keystore.p12' 文件添加到我的 springboot 项目 'resource' 文件夹并设置下面的 ssl 属性,但我不确定根据上述命令设置什么值。请帮助我设置下面的值,

server:
  ssl:
    key-store: classpath:keystore.p12
    key-store-password: ?
    key-store-type: ?
    key-alias: ?
    key-password: ?
    enabled: true
  port: ?

我还需要在我的 TOMCAT 服务器上做任何事情才能让 https 为这个 spring boot 项目工作吗???

【问题讨论】:

    标签: spring-boot ssl tomcat https war


    【解决方案1】:

    为 Spring Boot 试试这个:

    server:
      ssl:
        key-store: classpath:keystore.p12
        key-store-password: password2
        key-store-type: PKCS12
        key-alias: alias2
        enabled: true
      port: 443
    

    看起来没有设置密钥密码。尝试将其保留并尝试“changeit”(jks 的默认设置)。

    对于 TOMCAT,它需要在 $TOMCAT_HOME/conf/server.xml 中设置 - 连接器之一:

    <Connector
               protocol="org.apache.coyote.http11.Http11NioProtocol"
               port="443" maxThreads="200"
               scheme="https" secure="true" SSLEnabled="true"
               keystoreFile="path/to/keystore.p12" keystorePass="password2" keyAlias="alias2"
               clientAuth="false" sslProtocol="TLS"/>
    

    【讨论】:

    • 我应该把 keystore.p12 放在服务器的什么地方?
    • 我通常把 $TOMCAT_HOME/conf
    • 在此 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["https-jsse-nio-443"] SEVERE [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化失败后,我遇到了以下错误组件 [Connector[HTTP/1.1-443]] org.apache.catalina.LifecycleException:协议处理程序初始化失败,在 org.apache.catalina.connector.Connector.initInternal(Connector.java:983) .... Caused by: java .net.BindException:地址已在使用中......
    • 当我尝试 url 时出现以下错误 Not Found The requested URL was not found on this server。此外,在尝试使用 ErrorDocument 处理请求时遇到了 404 Not Found 错误。
    • “java.net.BindException:”是因为它正在运行,或者正在该端口上运行。
    【解决方案2】:

    最后我找到了解决方案。无需在 spring boot 上配置 SSL。只需在 tomcat 上配置 https,然后 https 即可在您的项目上运行。要在 tomcat 上配置 https,您需要生成一个密钥库文件(通常为 .jks 或 .p12 格式)通过使用 sslcert.crt(randomhex.crt),sslkey.key,sslCA.crt(gd_bundle-g2-g1.crt) 文件如下所示:

    转到服务器上的“/opt/apache-tomcat/conf/”并将上面/下面提到的文件放在那里

    openssl pkcs12 -export -in mycert.crt -inkey mykey.key -out mycert.p12 -name tomcat -CAfile myCA.crt -caname root -chain
    

    这里

    'mycert.crt' -> 你的 randomhex.crt 文件,

    'mykey.key' -> 来自 godaddy 的 SSL 密钥文件,

    'myCA.crt' -> gd_bundle-g2-g1.crt ,

    'mycert.p12' -> 您要生成的密钥库文件的名称。

    在运行上面的命令时,你应该询问密码,并记住该密码以配置 tomcat server.xml 文件。

    现在在编辑模式下打开 server.xml o tomcat conf 文件夹并在其中添加下面的连接器,然后退出并保存更改并重新启动 tomcat,然后 https 将开始处理您的项目。

    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" compression="on" scheme="https" secure="true" keystoreFile="conf/mycert.p12"
    keystorePass="password" SSLVerifyClient="none" SSLProtocol="TLSv1.2" />
    

    不要忘记在 spring boot 'application.yml' 上添加端口 8443

    server:
       port: 8443
    

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 1970-01-01
      • 2015-09-03
      • 2020-03-05
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      相关资源
      最近更新 更多