如何在Tomcat中启用HTTPS?

5

我有一个在tomcat上运行的web应用程序。现在我想强制使用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发生了什么? - user207421
@EJP 超时错误 - egjada
1个回答

10
为了默认启用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. 运行命令生成密钥库:

    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://<url> 请求重写为 https://<url>,这种情况会更简单些吗? - Marcello Romani
是的,这就是它的作用。你有读我的回答描述吗?请求将被重写为https://<URL>。 - Ahmedin Hassen
是的,我现在重新阅读了一遍。我的意思是我不想要“首先你需要正确安装/配置证书”,我只想将http重定向到https,证书将由其他组件处理。 - Marcello Romani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接