2015-09-28 123 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属性。

希望有帮助!