【问题标题】:How to redirect HTTP traffic to HTTPS while using Virtual Hosting in Tomcat如何在 Tomcat 中使用虚拟主机时将 HTTP 流量重定向到 HTTPS
【发布时间】:2019-09-15 06:33:37
【问题描述】:

我需要配置我的 Tomcat 9 服务器以将 http 重定向到 https 流量。

我试过了:

  1. 对 http 端口使用连接器并具有指向安全连接器的 redirectPort 属性。

  2. 在 web.xml 底部包含一个安全约束 link,它适用于其他未使用虚拟主机的 Tomcat 服务器

连接器

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

<Connector 
        port="443"
        protocol="org.apache.coyote.http11.Http11AprProtocol"
        secure="true"
        scheme="https" 
        maxThreads="200" 
        SSLEnabled="true" 
        maxSpareThreads="75" 
        maxHttpHeaderSize="8192" 
        acceptCount="100"  
        enableLookups="false" 
        disableUploadTimeout="true"
        defaultSSLHostConfigName="example1.com">
                        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
                        <SSLHostConfig hostName="example1.com">
                                        <Certificate 
                                        certificateKeystoreFile="www_example1_com.jks"
                                        certificateKeystorePassword="…” />
                        </SSLHostConfig>
                        <SSLHostConfig hostName="example2.com">
                                        <Certificate 
                                        certificateKeystoreFile="www_example2_com.jks"
                                        certificateKeystorePassword="…” />
                        </SSLHostConfig>
        </Connector>

安全约束

<security-constraint>
   <web-resource-collection>
      <web-resource-name>Entire Application</web-resource-name>
      <url-pattern>/*</url-pattern>
   </web-resource-collection>
   <user-data-constraint>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
   </user-data-constraint>
</security-constraint>

server.xml 中的主机配置

      <!-- example1.com -->
      <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" 
               directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>

      <!-- example2.com -->
      <Host name="example2.com" appBase="website-webapps" unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" 
               directory="website-logs"
               prefix="website_access_log." 
               suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>

【问题讨论】:

    标签: ssl redirect virtualhost tomcat9


    【解决方案1】:

    端口 80 未对我服务器上的外部流量开放,因此重定向永远不会发生。 http 流量没有到达服务器。

    已通过添加允许请求到达端口 80 的入站规则来解决此问题。

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 2019-08-13
      • 2017-04-08
      • 2020-12-12
      • 2014-04-11
      • 2012-07-22
      • 2018-10-12
      • 2014-01-19
      • 2018-09-23
      相关资源
      最近更新 更多