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

发布于 2024-12-02 06:27:37 字数 2356 浏览 1 评论 0原文

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

只需将 false 传递给 setDirectoriesListed 即可禁用 ResourceHandler 上的目录列表,按预期工作。有人可以告诉我如何为 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();
    }

}

I'm embedding Jetty (version 7.4.5.v20110725) into a java application. I'm serving JSP pages in ./webapps/jsp/ using Jetty's WebAppContext, but if I visit localhost:8080/jsp/ I get Jetty's directory listing for the entire contents of ./webapps/jsp/. I've tried setting the dirAllowed parameter to false on the WebAppContext and it does not change the directory listing behavior.

Disabling the directory listing on a ResourceHandler is simply done be passing false to setDirectoriesListed, works as expected. Can someone tell me how to do this for the 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();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

拥醉 2024-12-09 06:27:37

您可以设置 org.eclipse.jetty.servlet.Default.dirAllowed 代替 dirAllowed:

webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");

已针对 Jetty 7.4.5.v20110725、8.1.4.v20120524、9.0.2 进行测试.v20130417 和 9.2.0.v20140526。

You can set org.eclipse.jetty.servlet.Default.dirAllowed instead of dirAllowed:

webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");

Tested for Jetty 7.4.5.v20110725, 8.1.4.v20120524, 9.0.2.v20130417 and 9.2.0.v20140526.

你的他你的她 2024-12-09 06:27:37

对于任何使用 web.xml 的人,您也可以在那里禁止它。找到默认的 servlet(带有 Jetty 的 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>

For anyone using web.xml, you can also disallow it there. Find the default servlet (the one with Jetty's DefaultServlet), and set the dirAllowed parameter to 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>
听你说爱我 2024-12-09 06:27:37

这适用于 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>

This works for me on 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>
妄断弥空 2024-12-09 06:27:37

如果有人遇到这个问题,正在寻找 Jetty 6 中的等效项:

    <bean id="webAppContext" class="org.mortbay.jetty.webapp.WebAppContext">
    .
    .
    <property name="initParams">
        <map>               
            <entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
        </map>
    </property>

If anyone happens across this looking for the equivalent in Jetty 6:

    <bean id="webAppContext" class="org.mortbay.jetty.webapp.WebAppContext">
    .
    .
    <property name="initParams">
        <map>               
            <entry key="org.mortbay.jetty.servlet.Default.dirAllowed" value="false" />
        </map>
    </property>
友欢 2024-12-09 06:27:37

我在网上找到了描述相同问题的以下页面:

jetty-users-How-can-I-prevent-Directory-Listing-in-WebAppContext

I引用该帖子的其中一个条目中提到的问题原因:

问题是由于某种原因 Jetty 没有合并
使用嵌入模式时,webdefault.xml 与用户 web.xml 正确匹配

,以下是用于克服该问题的代码:

HashMap hmap = new HashMap<String, String>();
   hmap.put("dirAllowed", "false");
   hmap.put("redirectWelcome", "false");
   hmap.put("aliases", "false");
   ServletHolder []svh = wc.getServletHandler().getServlets();
   if(svh != null && svh.length > 0)
   {
           for(int j = 0; j < svh.length; j++)
      {
              ServletHolder svh1 = svh[j];
            if(svh1.getClassName() != null && svh1.getClassName().endsWith(DEFAULT_SERVLET))
            {
               svh1.setInitParameters(hmap);
             }
       }
   } 

我希望它能为您解决问题。

I found the following page on the net which describes the same problem:

jetty-users-How-can-I-prevent-Directory-Listing-in-WebAppContext

I quote what is mentioned in one of the entries in that post as reason for the problem:

the problem is that for some reason Jetty does not merge the
webdefault.xml with user web.xml properly when embedded mode is used

and following is the code that was used to overcome the problem:

HashMap hmap = new HashMap<String, String>();
   hmap.put("dirAllowed", "false");
   hmap.put("redirectWelcome", "false");
   hmap.put("aliases", "false");
   ServletHolder []svh = wc.getServletHandler().getServlets();
   if(svh != null && svh.length > 0)
   {
           for(int j = 0; j < svh.length; j++)
      {
              ServletHolder svh1 = svh[j];
            if(svh1.getClassName() != null && svh1.getClassName().endsWith(DEFAULT_SERVLET))
            {
               svh1.setInitParameters(hmap);
             }
       }
   } 

I hope it will solve the issue for you.

恰似旧人归 2024-12-09 06:27:37

到目前为止未提及的替代解决方案是添加 index.html 文件。也许这不是一个非常通用的解决方案,但它符合我的需求。附加值是,这对用户更加友好 - 意外输入应用程序 URL 的用户将获得您选择的人类可读描述,而不是 Jetty 的一些通用错误页面。

对我来说,这适用于嵌入式 Jetty 版本。 9.4.5。

我已将index.html 放在WEB-INF 目录旁边。

The alternative solution not mentioned so far is to add the index.html file. Probably this is not a very universal solution but it fitted my needs. The added value is that this is more user friendly - a user who accidentally enters your application URL will get human readable description of your choice instead of some generic error page from Jetty.

For me this worked with embedded Jetty ver. 9.4.5.

I've put index.html next to WEB-INF directory.

悍妇囚夫 2024-12-09 06:27:37

在 Linux 中使用 Jetty 9.2(但我认为它与 9.x 相同)适用于所有 Jetty 和基于 Jetty 的实例。

您可以在文件 /etc/jetty9/webdefault.xml 中进行更改:

<init-param>
  <param-name>dirAllowed</param-name>
  <param-value>false</param-value>
</init-param>

我也更改了:

<init-param>
     <param-name>welcomeServlets</param-name>
     <param-value>true</param-value>
  </init-param>
  <init-param>
     <param-name>redirectWelcome</param-name>
     <param-value>true</param-value>
  </init-param>

In Linux with Jetty 9.2 (but i think it's the same with 9.x) to apply to all Jetty and Jetty based instances.

You can change in file /etc/jetty9/webdefault.xml:

<init-param>
  <param-name>dirAllowed</param-name>
  <param-value>false</param-value>
</init-param>

I've also changed:

<init-param>
     <param-name>welcomeServlets</param-name>
     <param-value>true</param-value>
  </init-param>
  <init-param>
     <param-name>redirectWelcome</param-name>
     <param-value>true</param-value>
  </init-param>
暗喜 2024-12-09 06:27:37

另一种有效的方法是将此配置应用于 jetty-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
          "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <Call name="setInitParameter​">
    <Arg>org.eclipse.jetty.servlet.Default.dirAllowed</Arg>
    <Arg type="boolean">False</Arg>
  </Call>

</Configure>

Yet another method that works is applying this configuration to jetty-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
          "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <Call name="setInitParameter​">
    <Arg>org.eclipse.jetty.servlet.Default.dirAllowed</Arg>
    <Arg type="boolean">False</Arg>
  </Call>

</Configure>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文