本文介绍了Jetty 7-在根文件夹/下禁用目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每个服务器中都有很多运行RMI应用程序的生产服务器,另外还有4个Java Web应用程序,例如:

I have a lot of production servers running RMI application in each one, and more 4 Java webapps, eg:

Server A:
RMI app by JNLP file;
webapp_1 (connected by RMI with local RMI app);
webapp_2 (connected by RMI with local RMI app);
webapp_3 (connected by RMI with local RMI app);
webapp_4 (connected by RMI with local RMI app);

Server B:
...the same..OK

在默认上下文中,所有用户都直接在8080端口(直接到Jetty)上访问此服务器,例如主区域",它可以通过html链接访问所有应用程序(RMI应用程序,webapp_1,webapp_2等).

All users access this servers directly on 8080 port (direct to Jetty) in a default context, eg 'main-area', where it can access by some html links all apps (RMI app, webapp_1, webapp_2, etc.).

当某些用户访问"/"页面时,例如:

When some user access the '/' page, eg:

www.foo.com:8080/

main-area/
webapp_1/
webapp_2/
webapp_3/
...

Jetty返回一个包含所有应用程序的列表(就像Apache的目录列表一样).

Jetty returns a list with all applications (just like directory list of Apache).

是否有某种方法可以阻止它,或重定向到主区域"上下文?

Is there some way to block it, or redirect to 'main-area' context ?

推荐答案

不符合"/"的Webapp上下文列表将作为

The list of webapp contexts that do not match "/" is presented to you as part of the responsibility of org.eclipse.jetty.server.handler.DefaultHandler

DefaultHandler默认已启用,以保持与Servlet规范一致.

The DefaultHandler is enabled by default, to remain in conformance to the Servlet Spec.

禁用DefaultHandler:

如果只需要一个简单的404,而DefaultHandler没有提供任何信息,则只需在${jetty.home}/etc/jetty.xml

If you just want a simple 404, with no information presented by the DefaultHandler, then just comment it out in the ${jetty.home}/etc/jetty.xml

<!-- =========================================================== -->
<!-- Set handler Collection Structure                            --> 
<!-- =========================================================== -->
<Set name="handler">
  <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
    <Set name="handlers">
     <Array type="org.eclipse.jetty.server.Handler">
       <Item>
         <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
       </Item>
       <!-- Disable the DefaultHandler to avoid listing of non-matching contexts
       <Item>
         <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
       </Item>
        -->
     </Array>
    </Set>
  </New>
</Set>

"/"(ROOT)上下文呈现静态内容:

Presenting Static Content for "/" (ROOT) Context:

如果您希望根上下文"/"(ROOT)呈现其他内容,请创建一个${jetty.home}/webapps/ROOT目录并在其中放置一个index.html文件.

If you want the root context "/" (ROOT) to present something else, then create a ${jetty.home}/webapps/ROOT directory and put an index.html file in it.

[jetty-distribution-7.6.13.v20130916]$ cat webapps/ROOT/index.html 
<h1>This is ROOT</h1>

这将部署一个静态内容Web应用程序,您可以在其中放置想要的任何内容,图像,CSS等.

This will deploy a static content webapp where you can put any content you want to in there, images, css, etc.

自动将"/"(ROOT)重定向到另一个路径:

Automatically Redirecting "/" (ROOT) to another path:

注意:这将无法与上述${jetty.home}/webapps/ROOT选项,此选项或该选项同时使用,但不能同时使用.

Note: this will not work at the same time as the above ${jetty.home}/webapps/ROOT option, his this option, or that option, but not both.

如果您希望Jetty自动将"/"重定向到另一个URL,请使用重写处理程序.

If you want Jetty to redirect "/" automatically to another URL then use the rewrite handler.

确保已启用重写选项,并包括一组重写规则xml

Make sure you have the rewrite OPTION enabled, and include a set of rewrite rules xml

[jetty-distribution-7.6.13.v20130916]$ grep rewrite start.ini 
OPTIONS=Server,jsp,jmx,resources,websocket,ext,rewrite
etc/jetty-rewrite.xml

接下来,您将要定义您的重写规则...

Next, you'll want to define your rewrite rules ...

${jetty.home}/etc/jetty-rewrite.xml的内容,用于将访问从"/"重定向到"/test/"

Contents of ${jetty.home}/etc/jetty-rewrite.xml to redirect accesses from "/" to "/test/"

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
                   "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <Get id="oldhandler" name="handler"/>

    <Set name="handler">
     <New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
      <Set name="handler"><Ref id="oldhandler"/></Set>
      <Set name="rewriteRequestURI">true</Set>
      <Set name="rewritePathInfo">false</Set>
      <Set name="originalPathAttribute">requestedPath</Set>

      <!-- redirect from the welcome page to a specific page -->
      <Call name="addRule">
        <Arg>
          <New class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
            <Set name="regex">^/$</Set>
            <Set name="replacement">/test/</Set>
          </New>
        </Arg>
      </Call>
     </New>
    </Set>
</Configure>

这篇关于Jetty 7-在根文件夹/下禁用目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:13