Grizzly + 静态内容 + Servlet 过滤器 [英] Grizzly + Static Content + Servlet Filter

查看:89
本文介绍了Grizzly + 静态内容 + Servlet 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以让 Grizzly 提供静态内容

I can get Grizzly to serve static content

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

I can create the servlet filter to filter a named servlet

但是我无法让 servlet 过滤器过滤静态内容.我该怎么做?

But I can't get the servlet filter to filter the static content. How do I do that?

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

Here is the code I have so far:

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");

推荐答案

注册为 WebappContext(Web 应用程序)一部分的 ServletFilters 将仅针对与此 WebappContext(Web 应用程序)相关的请求执行.

The ServletFilters registered as part of WebappContext (Web application) will be executed only for requests related to this WebappContext (Web application).

所以,我看到的解决方案之一是在 WebappContext 上注册 DefaultServlet [1] 并使用它而不是 StaticHttpHandler.类似的东西:

So, one of the solutions I see is to register DefaultServlet [1] on the WebappContext and use it instead of StaticHttpHandler. Something like:

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

这篇关于Grizzly + 静态内容 + Servlet 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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