如何禁用Jetty的WebAppContext的目录列表?

pha*_*pus 22 java jetty embedded-jetty

我将Jetty(版本7.4.5.v20110725)嵌入到java应用程序中.我使用Jetty的WebAppContext在./webapps/jsp/中提供JSP页面,但如果我访问localhost:8080/jsp /我获取Jetty的目录列表,以获取./webapps/jsp/的全部内容.我已经尝试在WebAppContext上将dirAllowed参数设置为false,并且它不会更改目录列表行为.

在ResourceHandler上禁用目录列表就是将false传递给setDirectoriesListed,按预期工作.有人可以告诉我如何为WebAppContext执行此操作吗?

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;

public class Test {

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setHost("127.0.0.1");
        connector.setPort(8080);
        server.addConnector(connector);

        // Create a resource handler for static content.
        ResourceHandler staticResourceHandler = new ResourceHandler();
        staticResourceHandler.setResourceBase("./webapps/static/");
        staticResourceHandler.setDirectoriesListed(false);

        // Create context handler for static resource handler.
        ContextHandler staticContextHandler = new ContextHandler();
        staticContextHandler.setContextPath("/static");
        staticContextHandler.setHandler(staticResourceHandler);

        // Create WebAppContext for JSP files.
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath("/jsp");
        webAppContext.setResourceBase("./webapps/jsp/");
        // ??? THIS DOES NOT STOP DIR LISTING OF ./webapps/jsp/ ???
        webAppContext.setInitParameter("dirAllowed", "false");

        // Create a handler list to store our static and servlet context handlers.
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { staticContextHandler, webAppContext });

        // Add the handlers to the server and start jetty.
        server.setHandler(handlers);
        server.start();
        server.join();
    }

}
Run Code Online (Sandbox Code Playgroud)

ben*_*reg 42

您可以设置org.eclipse.jetty.servlet.Default.dirAllowed而不是dirAllowed:

webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
Run Code Online (Sandbox Code Playgroud)

测试Jetty 7.4.5.v20110725,8.1.4.v20120524,9.0.2.v20130417和9.2.0.v20140526.


Rea*_*ted 17

对于任何使用的人web.xml,你也可以在那里禁止它.找到默认的servlet(带有Jetty的servlet DefaultServlet),并将dirAllowed参数设置为false:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
    <init-param>
        <param-name>dirAllowed</param-name>
        <param-value>false</param-value>
    </init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)


Eng*_*uad 8

这适用于Jetty v9.4.3:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>org.eclipse.jetty.servlet.Default.dirAllowed</param-name>
        <param-value>false</param-value>
    </context-param>

</web-app>
Run Code Online (Sandbox Code Playgroud)