Getting 403 root requests using ResourceHandler and a custom handler in Jetty

In (embedded) Jetty, I am trying to use a ResourceHandler to serve static files and a custom handler to respond to dynamic requests. Based on this page I have a setting that looks like this:

public static void main(String[] args) throws Exception { Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(8080); server.addConnector(connector); ResourceHandler resource_handler = new ResourceHandler(); resource_handler.setDirectoriesListed(false); resource_handler.setResourceBase("."); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { resource_handler, new MyHandler() }); server.setHandler(handlers); server.start(); server.join(); } 

This works in the sense that it is correct:

  • Serves static content from files in my public directory, e.g. /public/style.css
  • Starts MyHandler along paths that are not in the public directory, for example / foo / bar

The problem is that I get 403 in response to the root path (/). MyHandler is able to respond to these requests, but they are first intercepted by the ResourceHandler. Is there a way to get Jetty to send / send MyHandler requests?

Thanks in advance!

+4
source share
2 answers

Jetty sequentially tries to process each handler until one of the handlers calls setHandled (true) in the request. I don’t know why ResourceHandler does not do this for "/".

My solution was to reverse the order in which you specify the handlers so that your call is called first. Then check the special url "/" in the url. If you want to pass the request to the ResourceHandler, just go back without declaring the request as processed.

Declare the order of the handlers as follows:

 Server server = new Server(8080); CustomHandler default = new CustomHandler(); default.setServer(server); ResourceHandler files = new ResourceHandler(); files.setServer(server); files.setResourceBase("./path/to/resources"); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] {default, files}); server.setHandler(handlers); server.start(); server.join(); 

And define the CustomHandler descriptor method as follows:

 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if(!request.getRequestURI().equals("/")){ return; } // Do Stuff... baseRequest.setHandled(true); return; } 

I agree that it would be very elegant if the ResourceHandler simply yields "/" instead of processing the response with 403.

+3
source

My decision:

  • puts MyHandler in a different context path than "/", for example. "/Index"
  • use rewrite rule to intercept calls to "/" and redirect them to "/ index"

The code I use looks like this:

 RewriteHandler rewriteHandler = new RewriteHandler(); rewriteHandler.setRewriteRequestURI(true); rewriteHandler.setRewritePathInfo(false); rewriteHandler.setOriginalPathAttribute("requestedPath"); RewriteRegexRule rewriteIndex = new RewriteRegexRule(); rewriteIndex.setRegex("^/$"); rewriteIndex.setReplacement("/index.html"); rewriteHandler.addRule(rewriteIndex); rewriteHandler.setHandler(rootHandlerCollection); server.setHandler(rewriteHandler); 

A regular expression ensures that only the exact path matches, so that "/ whatever" is still processed by the ResourceHandler.

+1
source

Source: https://habr.com/ru/post/1438224/


All Articles