2014-01-15 47 views
1

我能得到灰熊提供静态内容灰熊+静态内容+ Servlet过滤器

我可以创建Servlet过滤器来过滤命名的servlet

但我不能让Servlet过滤器来过滤静态内容。我怎么做?

这里是我到目前为止的代码:

WebappContext webappContext = new WebappContext("grizzly web context", ""); 
FilterRegistration authFilterReg = webappContext.addFilter("Authentication Filter", org.package.AuthenticationFilter.class); 

// If I create a ServletContainer, I can add the filter to it like this: 
// authFilterReg.addMappingForServletNames(EnumSet.allOf(DispatcherType.class), "servletName"); 

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI); 
webappContext.deploy(httpServer); 

// This works, but the content does not go through the authentication filter above 
httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler(absolutePath), "/static"); 

回答

1

注册为WebappContext(Web应用程序)的一部分,ServletFilters将只与此WebappContext(Web应用程序)的要求执行。

所以,我看到的解决方案之一是在WebappContext上注册DefaultServlet [1]并使用它代替StaticHttpHandler。喜欢的东西:

ArraySet<File> set = new ArraySet<File>(File.class); 
set.add(new File(absolutePath)); 
ServletRegistration defaultServletReg = webappContext.addServlet("DefaultServlet", new DefaultServlet(set) {}); 
defaultServletReg.addMapping("/static"); 

[1] https://github.com/GrizzlyNIO/grizzly-mirror/blob/2.3.x/modules/http-servlet/src/main/java/org/glassfish/grizzly/servlet/DefaultServlet.java

+0

请你在我的灰熊的问题我们来看一看:http://stackoverflow.com/questions/35123194/jersey-2-render-swagger-static-内容正确,没有尾随斜线 – DerekY