簡體   English   中英

在Apache Tomcat中執行301重定向從http到https

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

我在我的Web應用程序中配置了SSL。 我已根據所需步驟在我的Tomcat中安裝了證書。

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

我已經強制使用https over 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重定向,即永久重定向。

我怎樣才能做到這一點?

這是在您的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>

如果您的配置中沒有定義Realm標記,Tomcat將默認使用NullRealm 如果你想在這種情況下覆蓋重定向,你只需要在其下面定義一個NullRealm,並在其上設置transportGuaranteeRedirectStatus屬性。

希望有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM