【问题标题】:Perform 301 redirect from http to https in Apache Tomcat在 Apache Tomcat 中执行从 http 到 https 的 301 重定向
【发布时间】:2015-12-25 08:55:29
【问题描述】:

我已在我的 Web 应用程序中配置 SSL。我已按照所需步骤在我的 Tomcat 中安装了证书。

我一直在关注的教程是 https://www.mulesoft.com/tcat/tomcat-security

我已强制使用 https 而不是 http,这意味着对 http 的任何请求都将转发到 https。我在 server.xml 中进行了以下更改

<Connector port="8080" protocol="HTTP/1.1" 

           connectionTimeout="20000" 

           redirectPort="443"

           proxyHost="10.1.1.1" proxyPort="80"

           URIEncoding="UTF-8"

           maxHttpHeaderSize="32768"/>

web.xml更改如下:

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

但是,正在发生的重定向是临时重定向,即 302。 我想使用 301 重定向,即永久重定向。

我怎样才能做到这一点?

【问题讨论】:

标签: apache tomcat ssl


【解决方案1】:

这是在您的 Realm 上配置的。查看特定 Realm 实现的 transportGuaranteeRedirectStatus 属性。

https://tomcat.apache.org/tomcat-8.5-doc/config/realm.html

例如:server.xml 有这个开箱即用

  <Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
         resources under the key "UserDatabase".  Any edits
         that are performed against this UserDatabase are immediately
         available for use by the Realm.  -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase"/>
  </Realm>

它没有设置transportGuaranteeRedirectStatus,所以它默认为302。如果你想让它使用301,只需将属性transportGuaranteeRedirectStatus="301"添加到顶级Realm(根据你的配置,你可能没有嵌套Realms)并重新启动 Tomcat。

例如:

  <Realm className="org.apache.catalina.realm.LockOutRealm" transportGuaranteeRedirectStatus="301">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
         resources under the key "UserDatabase".  Any edits
         that are performed against this UserDatabase are immediately
         available for use by the Realm.  -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase" />
  </Realm>

如果您的配置中没有定义领域标签,Tomcat 将默认使用NullRealm。如果你想在这种情况下覆盖重定向,你只需要在下面定义一个 NullRealm 并设置 transportGuaranteeRedirectStatus 属性。

希望有帮助!

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 2016-01-17
    • 2018-04-09
    • 2019-12-17
    • 1970-01-01
    • 2016-01-04
    • 2020-03-19
    • 2013-09-27
    相关资源
    最近更新 更多