在Jetty中使用ResourceHandler和自定义处理程序时,在根请求上获取403 [英] Getting a 403 on root requests when using a ResourceHandler and custom handler in Jetty

查看:86
本文介绍了在Jetty中使用ResourceHandler和自定义处理程序时,在根请求上获取403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在(嵌入式)Jetty中,我试图使用ResourceHandler来提供静态文件,并使用自定义处理程序来响应动态请求.基于此页面,我有一个看起来像这样的设置:

In (embedded) Jetty, I'm 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 setup 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 correctly:

  • 从我的公共目录中的文件(例如/public/style.css)中提供静态内容
  • 在公共目录中不存在的路径上运行MyHandler,例如/foo/bar

问题是我收到403以响应根路径(/). MyHandler能够响应那些请求,但是它们首先会被ResourceHandler拦截.有什么方法可以强制Jetty向MyHandler发送/请求?

The problem is that I get a 403 in response to the root path (/). MyHandler is capable of responding to those requests, but they get intercepted by the ResourceHandler first. Is there any way to force Jetty to send / requests to MyHandler?

提前谢谢!

推荐答案

Jetty依次尝试每个处理程序,直到其中一个处理程序在请求上调用setHandled(true)为止.不确定为什么ResourceHandler不对"/"执行此操作.

Jetty tries each Handler sequentially until one of the handlers calls setHandled(true) on the request. Not sure why ResourceHandler doesn't do this for "/".

我的解决方案是颠倒您列出处理程序的顺序,以便首先调用您的处理程序.然后在URL中检查特殊情况"/".如果您希望将请求传递给ResourceHandler,只需返回而无需将请求声明为已处理即可.

My solution was to reverse the order in which you list the handlers so that yours is called first. Then check for the special case "/" in the URL. If you'd like to pass the request on to the ResourceHandler, simply return without declaring the request as handled.

像这样声明处理程序的顺序:

Declare the order of handlers like this:

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();

并定义CustomHandler的handle方法,如下所示:

And define CustomHandler's handle method like this:

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;
}

我同意让ResourceHandler仅仅在"/"上屈服而不是用403处理响应是最优雅的.

I agree it would be most elegant to have ResourceHandler simply yield on "/" instead of handling the response with a 403.

这篇关于在Jetty中使用ResourceHandler和自定义处理程序时,在根请求上获取403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆