在按照如何让Jetty将httpredirect到https的build议后,我们能够使我们的独立Jetty部署将所有HTTP请求redirect到HTTPS。 然而,redirect是一个302.我们怎样才能使这个301(永久)redirect呢?
作为参考,我们将其添加到Jetty的etc / webdefault.xml中:
<web-app> ... <security-constraint> <web-resource-collection> <web-resource-name>Everything in the webapp</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> </web-app>
我们可以通过在战争旁边添加一个redirector.xml文件来获得所需的行为。 该文件的内容如下。 请注意,这也会将example.comredirect到www.example.com:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.server.handler.MovedContextHandler"> <Set name="contextPath">/</Set> <Set name="newContextURL">https://www.example.com</Set> <Set name="permanent">true</Set> <Set name="discardPathInfo">false</Set> <Set name="discardQuery">false</Set> <Set name="virtualHosts"> <Array type="String"> <Item>example.com</Item> </Array> </Set> </Configure>