Jetty:将HTTP重定向到HTTPS以获取静态内容

Sil*_*eri 13 java xml jetty jetty-9

我已经使用两个XML上下文配置设置了Jetty 9.3.一个用于静态内容:

<?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>
Run Code Online (Sandbox Code Playgroud)

一个用于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>
Run Code Online (Sandbox Code Playgroud)

然后,我使用此答案设置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>
Run Code Online (Sandbox Code Playgroud)

并将以下内容添加到我的HttpConfigurationjetty/etc/jetty.xml:

<Call name="addCustomizer">
  <Arg>
    <New class="org.eclipse.jetty.server.SecureRequestCustomizer" />
  </Arg>
</Call>
Run Code Online (Sandbox Code Playgroud)

这适用于我的Web应用程序(即通过'/ webapp'上的HTTP访问服务器将重定向到HTTPS),但似乎不会影响'/ static'下提供的静态内容.我认为这是因为添加的设置webdefault.xml仅适用于Web应用程序,因为它们具有适用的web.xml文件.

如何为我作为静态内容的所有网页设置HTTP请求以重定向到HTTPS?

D. *_*ács 1

据我所知(例如,基于此 SO此 SF以及Jetty Docs)它不可配置为静态内容,仅适用于网络应用程序。

可以做的(这并不意味着您应该这样做)是,@PreMatching如果您使用的是 JAX-RS,则创建一个自定义过滤器;如果您使用的是 JAX MessageHandler-WS,则创建一个自定义过滤器,该过滤器以编程方式执行重定向(例如,通过返回 HTTP 301)。