12

我正在使用嵌入式 Jetty,我想添加一个 servlet 过滤器以在每个请求之前检查身份验证。我尝试按照此示例进行操作,但看起来签名已更改。

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

我的 Jetty 启动器如下所示:

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() {}

}
4

1 回答 1

22

您可能正在寻找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() {}
  }
}
于 2013-10-23T16:44:12.387 回答