Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty: Redirect HTTP to HTTPS for static content

I have set up Jetty 9.3 with two XML context configurations. One for 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>

and one for a web application (WAR file):

<?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>

I then used this answer to set up Jetty to forward HTTP requests to HTTPS. More specifically, I added the following to 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>

and added the following to my HttpConfiguration in jetty/etc/jetty.xml:

<Call name="addCustomizer">
  <Arg>
    <New class="org.eclipse.jetty.server.SecureRequestCustomizer" />
  </Arg>
</Call>

This works perfectly for my web application (i.e. accessing the server through HTTP at '/webapp' will redirect to HTTPS), but doesn't seem to affect the static content served under '/static'. I assume this is because the setting added to webdefault.xml only applies to web applications since they have an applicable web.xml file.

How can I set up HTTP requests to redirect to HTTPS for all my pages served as static content?

like image 350
Silveri Avatar asked Jun 28 '16 10:06

Silveri


People also ask

How do I redirect http to https in Jetty?

Set up Jetty 9 to redirect all HTTP requests to HTTPS instead of disabling the HTTP connector after Enabling SSL in verison 5.2 and later.

What is http jetty?

Jetty provides a web server and servlet container, additionally providing support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and are freely available for commercial use and distribution.

How to add static web content in Jetty web server?

This approach requires Jetty 9.3.x and up. All you need to do is put an XML file into the webapps directory, then start Jetty Web Server up. This XML file will direct the Jetty Web Server to a directory where static web content can be found and served to end user who request for them. Here is a sample XML file. I call it junk.xml.

What is the point of using Jetty web server?

If this does not satisfy one's need, one can also use a HTTP web server like Apache Http Web Server, or Ngnix. Alternatively, one can use Python, Ruby on Rail or Node JS to start a light weigh web server. So what is the point of using Jetty Web Server to simply deliver static web content? The point is convenience.

How to redirect HTTP to HTTPS using htaccess file?

Now, you know how to edit the .htaccess file, let’s redirect HTTP to HTTPS using the edit .htaccess file method. 1. Redirect All Web Traffic Add this code below the existing code in your .htaccess file. 2. Redirect Only a Specific Domain Add this code to redirect a specific domain to use HTTPS. 3. Redirect Only a Specific Folder

How to get static web content from a J2EE container?

Any J2EE container, like Jetty or Tomcat or etc, had a DefaultHandler that could deliver static web contents to the user by http requests. That is what this web.xml is specifying. In this web.xml, there is the section "servlet-mapping". The element "servlet-name" is set to default.


1 Answers

As far as I could tell (e.g., based on this SO and this SF and the Jetty Docs) it's not configurable for static content, only for webapps.

What you could do (that does not mean that you should do it this way) is that you create a custom @PreMatching filter if you are using JAX-RS or a custom MessageHandler if you are using JAX-WS which does the redirection programatically (e.g., through returning an HTTP 301).

like image 99
D. Kovács Avatar answered Oct 19 '22 07:10

D. Kovács