【问题标题】:tomcat ssl redirect looptomcat ssl重定向循环
【发布时间】:2017-04-10 07:39:18
【问题描述】:

我的应用程序在tomcat中使用ssl,当我使用端口8080时运行良好,当我访问http://localhost:8080时,它重定向到https://localhost:8443

但是当我使用非 8080 端口(8081,8082 等)并访问 http://localhost:8081(或 http://localhost:8082 等)时,它会无限重定向循环到 http://localhost:8081(或 http://localhost:8082 等) )

这里是带有 8080 的 server.xml

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />


            <Connector SSLEnabled="true" clientAuth="false" keyAlias="key" keystoreFile="webapps/ROOT/META-INF/my.keystore" keystorePass="pass" maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS"/>   


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">


      </Host>
    </Engine>
  </Service>
</Server>

这里我的 server.xml 带有 8081

    <?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <Connector port="8081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />


            <Connector SSLEnabled="true" clientAuth="false" keyAlias="key" keystoreFile="webapps/ROOT/META-INF/my.keystore" keystorePass="pass" maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS"/>   


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">


      </Host>
    </Engine>
  </Service>
</Server>

谁能帮帮我?

我正在使用 tomcat 6

【问题讨论】:

    标签: java tomcat ssl


    【解决方案1】:

    要尝试的另一件事是在默认 web.xml 中为所有应用程序强制启用 HTTPS(它应该在 server.xml 旁边的 tomcat/conf 文件夹中), 通过添加以下内容:

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

    注意: 以下内容适用于启用 ARP 的 Tomcat 实例(即大多数基于 Linux 的安装),但由于 OP 正在运行基于 Windows 的 Tomcat,且 ARP 连接器已禁用(或不可用),它不适用于他。


    不确定为什么会出现重定向循环,但配置的一个明显问题是您启用了APR(本机)SSL 连接器,它实际上不适用于“keystoreFile”,所以您应该在您的日志中收到类似的错误消息:

    java.lang.Exception: Connector attribute SSLCertificateFile must be defined when using SSL with APR 
    

    无论使用什么端口号,SSL 都不应该为您工作。

    打败它的最简单方法是注释掉 APR 监听器:

    <!-- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> -->
    

    让 Tomcat 使用 JSSE 连接器实现。

    【讨论】:

    • 感谢您的回答。我既没有使用端口 8080 也没有使用端口 8081 在日志中没有收到任何错误。我的应用程序在端口 8080 中的 ssl 上运行良好。但是我也测试了 APR 侦听器注释掉了,没有任何变化,没有错误并使用端口 8081 重定向循环。我使用的是 Tomcat 6。
    • 好的,我明白了,所以您可能只是在第一种情况下没有安装 ARP。您正在运行哪个操作系统/确切版本的 Tomcat?您的配置看起来像 Tomcat 6(但我可能错了)。我已经尝试使用您的配置运行 Tomcat 6(通过官方 Docker 映像),除了我必须禁用 ARP(默认情况下对我来说是打开的)之外,它工作得很好(即没有重定向循环)。
    • 您的 Tomcat 实例(Nginx 等)前面还有什么东西吗?
    • 不,我没有在 Tomcat 前使用任何东西。我在 Windows 10 和 Windows server 2012 中进行了测试,两者都存在同样的问题。在端口 8080 中正确重定向,在端口 8081 中进行循环重定向。我的应用程序使用 spring mvc
    • 您能否确认尝试访问其他文件或应用程序也会导致重定向循环(即这不是您的 Spring 应用程序特定的)?您可以尝试转到localhost:8081/index.html(Tomcat 索引页面,如果您没有删除它),或localhost:8081/manager/status(如果您安装了 Tomcat Manager 应用程序)。另外,当您直接转到:8443 时,它是否仍然适合您(以 8081 作为 HTTP 端口)?
    猜你喜欢
    • 2014-03-18
    • 2015-02-26
    • 1970-01-01
    • 2018-03-08
    • 2015-02-15
    • 1970-01-01
    • 2014-06-01
    • 2014-09-18
    • 1970-01-01
    相关资源
    最近更新 更多