【问题标题】:How to enforce https with tomcat?如何使用tomcat强制执行https?
【发布时间】:2017-09-01 06:50:35
【问题描述】:

我有一个在 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" />

<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>

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

【问题讨论】:

  • 端口 8443 会发生什么?
  • @EJP 超时错误

标签: tomcat https


【解决方案1】:

要默认强制使用 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>

**

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

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

  1. 生成的keystore运行命令:

    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" />

【讨论】:

  • 如果想将http://&lt;url&gt; 请求重写为https://&lt;url&gt; 的更简单的场景呢?
  • 是的,这就是它的作用。您是否阅读了我的答案的描述?请求将被重写为 https://
  • 是的,我现在重新阅读它。我的意思是我不想first you need to install/configure your certificates properly,我只想将http重定向到https,证书将由其他组件处理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2011-11-23
相关资源
最近更新 更多