我已经使用两个XML上下文配置设置了Jetty 9.3。一个为static content:
<?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.ContextHandler">
<Set name="contextPath">/static</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="resourceBase">/home/user/static</Set>
<Set name="directoriesListed">true</Set>
</New>
</Set>
</Configure>
一个用于Web应用程序(WAR文件):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/webapp</Set>
<Set name="war">/home/user/webapp.war</Set>
</Configure>
然后我使用this answer设置Jetty以将HTTP请求转发到HTTPS。更具体地说,我在jetty/etc/webdefault.xml
中添加了以下内容:
<security-constraint>
<web-resource-collection>
<web-resource-name>Everything</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
并将以下内容添加到我在HttpConfiguration
的jetty/etc/jetty.xml
中:
<Call name="addCustomizer">
<Arg>
<New class="org.eclipse.jetty.server.SecureRequestCustomizer" />
</Arg>
</Call>
这适用于我的Web应用程序(即通过'/ webapp'上的HTTP访问服务器将重定向到HTTPS),但似乎不会影响在'/ static'下提供的静态内容。我认为这是因为添加到webdefault.xml
的设置仅适用于Web应用程序,因为它们具有适用的web.xml
文件。
如何为我作为静态内容的所有网页设置HTTP请求以重定向到HTTPS?
据我所知(例如,基于this SO和this SF以及Jetty Docs),它不能配置静态内容,仅适用于webapps。
你能做什么(这并不意味着你应该这样做)是你创建一个自定义@PreMatching
过滤器,如果你使用JAX-RS或自定义MessageHandler
如果你使用JAX-WS以编程方式进行重定向(例如,通过返回HTTP 301)。