Java/Jetty:如何向嵌入式Jetty添加过滤器 [英] Java / Jetty: How to Add Filter to Embedded Jetty

查看:140
本文介绍了Java/Jetty:如何向嵌入式Jetty添加过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with embedded Jetty and I want to add a servlet filter to check for authentication before each request. I tried following this example but it looks like the signature has changed.

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

我的Jetty启动器看起来像这样:

My Jetty starter looks like this:

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,如何添加一个在请求开始时运行的过滤器,并仅在满足某些条件时才允许请求继续进行?

I find that the signature has changed. So I want to take a small step back and ask, with embedded Jetty, how do I add a filter that runs at the beginning of the request and allows the request to continue only if some condition is met?

AuthenticationFilter类的开始看起来像这样:

The beginning of the AuthenticationFilter class looks like this:

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

}

推荐答案

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

You are probably looking for EnumSet.of(DispatcherType.REQUEST), included a full example below:

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

这篇关于Java/Jetty:如何向嵌入式Jetty添加过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆