Java / Jetty:如何向嵌入式Jetty添加过滤器

时间:2013-10-23 00:41:16

标签: java spring jetty embedded-jetty

我正在使用嵌入式Jetty,我想在每次请求之前添加一个servlet过滤器来检查身份验证。我尝试了this example,但看起来签名已经改变了。

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.0.4.v20130625</version>
</dependency>

我的码头首发看起来像这样:

public class JettyStarter {

    public static void main( final String[] args ) throws Exception {
        Server server = new Server(8080);
        final ServletHolder servletHolder = new ServletHolder(new CXFServlet());
        final ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        // context.addFilter(AuthenticationFilter.class, "/*", FilterMapping.REQUEST);
        context.addServlet(servletHolder, "/platform/*");
        context.addEventListener(new ContextLoaderListener());
        context.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
        context.setInitParameter("contextConfigLocation", Config.class.getName());
        server.setHandler(context);
        server.start();
        server.join();
    }
}

当我取消注释该行

// context.addFilter(AuthenticationFilter.class, "/*", FilterMapping.REQUEST);

我发现签名已经改变了。所以我想稍微退后一步,用嵌入式Jetty询问如何添加在请求开始时运行的过滤器,并且只有在满足某些条件时才允许请求继续?

AuthenticationFilter类的开头如下所示:

import javax.servlet.*;
import java.io.IOException;

public class AuthenticationFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, 
                         ServletResponse servletResponse, 
                         FilterChain filterChain) throws IOException, ServletException {}

    @Override
    public void destroy() {}

}

1 个答案:

答案 0 :(得分:17)

您可能正在寻找EnumSet.of(DispatcherType.REQUEST),其中包含以下完整示例:

import java.io.IOException;
import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;

public class JettyFilter {

  public static void main(final String[] args) throws Exception {
    Server server = new Server(8080);

    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);

    handler.addServletWithMapping(HelloServlet.class, "/*");
    handler.addFilterWithMapping(HelloPrintingFilter.class, "/*",
        EnumSet.of(DispatcherType.REQUEST));

    server.start();
    server.join();
  }

  public static class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println("<h1>Hello SimpleServlet</h1>");
    }
  }

  public static class HelloPrintingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
      System.out.print("hello from filter");
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

    @Override
    public void destroy() {}
  }
}