Jetty 7 - Disable directory list at root folder /

2019-06-10 16:45发布

问题:

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

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 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 ?

回答1:

Create an index.html file for that location.

This will be served so the list does not need to be generated.

You can then put a simple redirect in there, along with a normal link if the browser does not respect redirects.



回答2:

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

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

Disabling DefaultHandler:

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>

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

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>

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

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

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.

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

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 ...

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>


回答3:

Thanks to Thorbjørn Ravn Andersen by solution:

I created a basic Dynamic Web Application with a single index.jsp with JSP HTML/JS redirect inside:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=bar/index.jsp">
        <script type="text/javascript">
            window.location.href = "bar/index.jsp"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        If you are not redirected automatically, follow the <a href='bar/index.jsp'>main area</a>
    </body>
</html>

I deployed as 'ROOT.war' and I configured a 'root.xml' in '...jetty/contexts/' with:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/ROOT.war</Set>
</Configure>


标签: java Jetty