1

I have an embedded Jetty Server and I want to direct request from http://localhost:8080/ to https://localhost:4433/ how do I do that? I tried this:

    // Set up rewriting (HTTP -> HTTPS)
    RewriteHandler rewrite = new RewriteHandler();

    RedirectPatternRule redirect = new RedirectPatternRule();
    redirect.setPattern("http://localhost:8080/*");
    redirect.setLocation("https://localhost:4433/");  
    rewrite.addRule(redirect);

And added it within my handler, but it doesn't work. I know rules like

    RewriteHandler rewrite = new RewriteHandler();

    RedirectPatternRule redirect = new RedirectPatternRule();
    redirect.setPattern("/redirect/*");
    redirect.setLocation("/redirected");  
    rewrite.addRule(redirect);

work 100%, but is there a way to match the full URL?

Display
  • 314
  • 1
  • 13

2 Answers2

2

The RewriteHandler only works with paths.

Not schemes, hosts, or ports.

You can use the SecuredRedirectHandler built into the jetty-server artifact since Jetty 9.2. (Be sure you have a valid HttpConfiguration setup for both of your connectors)

.. or ..

You could use the Servlet constraints to enforce it to be confidential.

See buildConstraintSecurityHandler() in the answer to how to programatically enforce security-constraint in web.xml

Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
0

A more general approach that is not specific to Jetty is to return a 301 with a new URL:

GET /path/location.html HTTP/1.1
Host: example.com

gets turned into

HTTP/1.1 301 Moved Permanently
Location: https://example.com/path/location.html

This can be done in Jetty and most server frameworks by simply setting the response status and adding the location header in a filter if you dont want the resource to be accessible through plain-text http.

nikdeapen
  • 1,581
  • 3
  • 15
  • 27