简体   繁体   English

在Apache Tomcat中执行301重定向从http到https

[英]Perform 301 redirect from http to https in Apache Tomcat

I have configured SSL in my web application. 我在我的Web应用程序中配置了SSL。 I have installed the certificate in my Tomcat as per the required steps. 我已根据所需步骤在我的Tomcat中安装了证书。

The tutorial that I have been following is https://www.mulesoft.com/tcat/tomcat-security 我一直关注的教程是https://www.mulesoft.com/tcat/tomcat-security

I have enforced the use of https over http which means that any request to http will be forwarded to https. 我已经强制使用https over http,这意味着对http的任何请求都将转发到https。 I made the following changes in my server.xml 我在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"/>

The web.xml changes are as follows: 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>

However, the redirect that is taking place is temporary re-direct ie 302. I want to use 301 re-direct ie., permanent redirect. 但是,正在发生的重定向是临时重定向,即302.我想使用301重定向,即永久重定向。

How can I achieve that? 我怎样才能做到这一点?

This is configured on your Realm. 这是在您的Realm上配置的。 See the transportGuaranteeRedirectStatus attribute of your particular Realm implementation. 请参阅特定Realm实现的transportGuaranteeRedirectStatus属性。

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

Ex: server.xml has this out-of-the-box 例如: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>

It does not set transportGuaranteeRedirectStatus so it defaults to 302. If you want to make it use a 301, just add the attribute transportGuaranteeRedirectStatus="301" to the top level Realm (you may not have nested Realms depending on your configuration) and restart Tomcat. 它没有设置transportGuaranteeRedirectStatus所以它默认为302.如果你想让它使用301,只需将属性transportGuaranteeRedirectStatus="301"到顶层Realm(你可能没有嵌套的Realms,具体取决于你的配置)并重启Tomcat 。

Ex: 例如:

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

If you do not have a Realm tag defined in your configuration, Tomcat will default to using a NullRealm . 如果您的配置中没有定义Realm标记,Tomcat将默认使用NullRealm If you want to override the redirect in this situation, you'd just need to define a NullRealm under with the transportGuaranteeRedirectStatus property set on it. 如果你想在这种情况下覆盖重定向,你只需要在其下面定义一个NullRealm,并在其上设置transportGuaranteeRedirectStatus属性。

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM