如何禁用 Jetty 的 WebAppContext 的目录列表?
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以设置 org.eclipse.jetty.servlet.Default.dirAllowed 代替 dirAllowed:
已针对 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 ofdirAllowed
:Tested for Jetty 7.4.5.v20110725, 8.1.4.v20120524, 9.0.2.v20130417 and 9.2.0.v20140526.
对于任何使用
web.xml
的人,您也可以在那里禁止它。找到默认的 servlet(带有 Jetty 的DefaultServlet
的那个),并将dirAllowed
参数设置为false
:For anyone using
web.xml
, you can also disallow it there. Find the default servlet (the one with Jetty'sDefaultServlet
), and set thedirAllowed
parameter tofalse
:这适用于 Jetty v9.4.3:
web.xml:
This works for me on Jetty v9.4.3:
web.xml:
如果有人遇到这个问题,正在寻找 Jetty 6 中的等效项:
If anyone happens across this looking for the equivalent in Jetty 6:
我在网上找到了描述相同问题的以下页面:
jetty-users-How-can-I-prevent-Directory-Listing-in-WebAppContext
I引用该帖子的其中一个条目中提到的问题原因:
,以下是用于克服该问题的代码:
我希望它能为您解决问题。
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:
and following is the code that was used to overcome the problem:
I hope it will solve the issue for you.
到目前为止未提及的替代解决方案是添加 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.
在 Linux 中使用 Jetty 9.2(但我认为它与 9.x 相同)适用于所有 Jetty 和基于 Jetty 的实例。
您可以在文件
/etc/jetty9/webdefault.xml
中进行更改:我也更改了:
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
:I've also changed:
另一种有效的方法是将此配置应用于 jetty-web.xml:
Yet another method that works is applying this configuration to
jetty-web.xml
: