我正在使用嵌入式 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() {}
}