2015-09-28 120 views
10

我在我的web應用程序中配置了SSL。根據所需的步驟,我已將證書安裝在Tomcat中。在Apache Tomcat中執行301從http重定向到https

,我已被以下的教程是 https://www.mulesoft.com/tcat/tomcat-security

我已經強迫通過http使用https這意味着爲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重定向即永久重定向。

我該如何做到這一點?

+0

你是否找到了這個問題的答案?我也有同樣的問題。 – Thermometer

+0

同樣在這裏,在這個問題上的任何進展? – Default71721

+0

對於Google員工現在只想「強制執行https tomcat」,「始終https tomcat」或類似的情況,這就是解決方案。 https://jelastic.zendesk.com/hc/en-us/community/posts/206121996-HTTP-HTTPS-redirection-into-the-Tomcat也提供了一個解決方案。 – koppor

回答

2

這是在您的領土上配置的。請參閱特定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> 

如果你想讓它使用301它不會設置transportGuaranteeRedirectStatus所以它默認爲302外的開箱,只需將屬性transportGuaranteeRedirectStatus="301"添加到頂層領域(根據您的配置,您可能沒有嵌套領域)並重新啓動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屬性。

希望有幫助!