【问题标题】:Grizzly + Static Content + Servlet FilterGrizzly + 静态内容 + Servlet 过滤器
【发布时间】:2014-01-15 03:08:44
【问题描述】:

我可以让 Grizzly 提供静态内容

我可以创建 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");

【问题讨论】:

    标签: java servlets servlet-filters grizzly


    【解决方案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

    【讨论】:

    猜你喜欢
    • 2011-04-28
    • 2014-08-22
    • 2014-01-22
    • 2013-12-28
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2013-06-02
    • 2011-11-08
    相关资源
    最近更新 更多