如何使用tomcat强制执行https?

egj*_*ada 2 https tomcat

我有一个在 tomcat 上运行的 webapp。现在我想强制执行 https。我收到了 3 个保存在 tomcat/conf/ 中的文件:localhost-rsa-cert.pem、localhost-rsa-chain.pem 和 localhost-rsa-key.pem

我更改了 server.xml 使未注释的连接器看起来像这样并重新启动了 tomcat。我只能使用 ...com:8080 而不是 ...com:8443 访问我的页面。问题是什么?

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
Run Code Online (Sandbox Code Playgroud)
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
           maxThreads="150" SSLEnabled="true" >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig>
        <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                     certificateFile="conf/localhost-rsa-cert.pem"
                     certificateChainFile="conf/localhost-rsa-chain.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>
Run Code Online (Sandbox Code Playgroud)

我读到您也需要更改 web.xml 中的某些内容。

小智 5

要默认强制使用 https,您需要将以下内容添加到您的 web.xml(这是与 server.xml 位于同一目录中的根 web.xml)。这将强制将所有请求重定向到https://www.yoursite.com,即使用户输入http://www.yoursite.com

**

<security-constraint>
 <web-resource-collection>
 <web-resource-name>Protected Context</web-resource-name>
 <url-pattern>/*</url-pattern>
 </web-resource-collection>
 <user-data-constraint>
 <transport-guarantee>CONFIDENTIAL</transport-guarantee>
 </user-data-constraint>
 </security-constraint>
Run Code Online (Sandbox Code Playgroud)

**

但是首先您需要正确安装/配置您的证书,并且您需要确保在您手动输入 https 时可以访问您的应用程序 https。

我已按照以下步骤在 tomcat 8 上安装和配置 SSL。

  1. 生成的密钥库运行命令:

    keytool -keysize 2048 -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore

    通过运行命令生成 csr 文件:

    keytool -certreq -keyalg RSA -alias tomcat -file yourCSR.csr -keystore tomcat.keystore

    在 www.godaddy.com 上提交了 CSR 请求

一旦我的请求获得批准并且 goDaddy 颁发了证书。我已按照以下步骤操作(goDaddy 将此说明与证书文件一起发送)。通过运行以下命令安装根证书:

keytool -import -alias root -keystore tomcat.keystore -trustcacerts -file [name of the root certificate]

通过运行以下命令安装中间证书:

keytool -import -alias intermed -keystore tomcat.keystore -trustcacerts -file [name of the intermediate certificate] 我的中间证书以 .pem 结尾

通过运行以下命令将颁发的证书安装到密钥库中:

keytool -import -alias tomcat -keystore tomcat.keystore -trustcacerts -file [name of the certificate]

最后,我将以下内容添加/修改到 server.xml 并重新启动了 tomcat 服务器。

<Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" />


<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
   maxThreads="150" scheme="https" secure="true"
   clientAuth="false" sslProtocol="TLS" keystoreFile=".mykeystore" keystorePass="xxxxxx"
   ciphers="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
   TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,
   TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,
   TLS_RSA_WITH_AES_256_CBC_SHA,SSL_RSA_WITH_RC4_128_SHA" />
Run Code Online (Sandbox Code Playgroud)