Embedded Jetty rewrites not working properly

2019-04-20 21:52发布

问题:

I am trying to implement a simple rewrite rule in an embedded Jetty server, following the Jetty documentation examples.

I want requests to /admin/ to rewrite to /admin.html. At the moment if I request /admin/ I get a 404 error with /admin.html not found. But if I request /admin.html directly, it works!

There are 2 other similar posts on stackoverflow but no answers to the question!

Here's the code:

WebAppContext _ctx = new WebAppContext();
_ctx.setContextPath("/");
_ctx.setDefaultsDescriptor(JETTY_DEFAULTS_DESCRIPTOR);
_ctx.setParentLoaderPriority(true);       
_ctx.setWar(getShadedWarUrl());
_ctx.setResourceBase(getShadedWarUrl());

RewriteHandler rewriter = new RewriteHandler();
rewriter.setRewritePathInfo(true);
rewriter.setRewriteRequestURI(true);
rewriter.setOriginalPathAttribute("requestedPath");

RewritePatternRule admin = new RewritePatternRule();
admin.setPattern("/admin/");
admin.setReplacement("/admin.html");
admin.setTerminating(true); // this will stop Jetty from chaining the rewrites
rewriter.addRule(admin);

_ctx.setHandler(rewriter);

HandlerCollection _handlerCollection = new HandlerCollection();
_handlerCollection.setHandlers(new Handler[] {_ctx});
server.setHandlers(_result);

回答1:

Replace the 2 lines ...

_ctx.setHandler(rewriter);
_handlerCollection.setHandlers(new Handler[] {_ctx});

with

rewriter.setHandler(_ctx);
_handlerCollection.setHandlers(new Handler[] {rewriter});

That will make the rewriter rules kick in before the normal context handling.

Think of the context handling as a tree. In your example code you have ....

server
+--  HandlerCollection
     [0]-- WebAppContext
           +-- Your servlets and filters in web.xml
           +-- DefaultServlet
               +-- RewriteHandler

That means if the WebAppContext can't handle the request, then the RewriteHandler is executed to see if it can handle the request. That will never happen, as WebAppContext is setup to use DefaultServlet if nothing else matches.

The simple change suggested changes the tree to look like this ...

server
+--  HandlerCollection
     [0]-- RewriteHandler
           +-- WebAppContext
               +-- Your servlets and filters in web.xml
               +-- DefaultServlet

This will allow the RewriteHandler to do its thing before the WebAppContext is even asked.

Note: you can also have your code utilize the HandlerCollection a bit more properly for this scenario as well.

// _ctx.setHandler(rewriter);
// rewriter.setHandler(_ctx);
_handlerCollection.setHandlers(new Handler[] { rewriter, _ctx });

This will result in the following tree

server
+--  HandlerCollection
     [0]-- RewriteHandler
     [1]-- WebAppContext
           +-- Your servlets and filters in web.xml
           +-- DefaultServlet