有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Felix OSGi Servlet/资源筛选器不进行筛选

我试图过滤OSGi ApacheFelix中Jetty HTTP服务器的web资源

我已在框架中将过滤器注册为OSGi组件:

@Component(property = { "osgi.http.whiteboard.filter.name=MyFilter",
                        "osgi.http.whiteboard.filter.regex=.*" },
           scope = ServiceScope.PROTOTYPE)
public class MyFilter implements Filter {
    ...
    @Override
    public void doFilter(ServletRequest request, 
                         ServletResponse response,
                         FilterChain chain) {
       ...
       // some logging
    }
    ...
}

当我启动Felix框架并通过浏览器访问资源和servlet时,Filter#doFilter(...)方法永远不会被调用

资源和servlet已使用org.osgi.service.http.HttpService#registerServlet(...)org.osgi.service.http.HttpService#registerResources(...)注册

我敢肯定,由于调用了Filter#init(...),过滤器将被初始化:

以下是部件的Felix scr信息:

Component Description:
  Name: org.myCompany.MyFilter
  Implementation Class: org.myCompany.MyFilter
  Default State: enabled
  Activation: delayed
  Configuration Policy: optional
  Activate Method: activate
  Deactivate Method: deactivate
  Modified Method: -
  Configuration Pid: [org.myCompany.MyFilter]
  Services: 
    javax.servlet.Filter
  Service Scope: prototype
  Component Description Properties:
      osgi.http.whiteboard.filter.name = MyFilter
      osgi.http.whiteboard.filter.regex = .*
  Component Configuration:
    ComponentId: 7
    State: active      
    Component Configuration Properties:
        component.id = 7
        component.name = org.myCompany.MyFilter
        osgi.http.whiteboard.filter.name = MyFilter
        osgi.http.whiteboard.filter.regex = .*
  • 资源/servlet过滤在Felix、Jetty和OSGi中起作用吗
  • 我是否正确注册了过滤器

同时,感谢您的关注和参与


共 (1) 个答案

  1. # 1 楼答案

    R6规范不清楚HttpService和白板服务之间的交互。事实上,RFC 0223的更新显示:

    3.1 Whiteboard Services and Http Service (Bug 2872)

    If a Http Whiteboard implementation is also implementing the Http Service, the whiteboard specification does not specify whether the Http contexts for the Http Service are represented as ServletContextHelper services. There is no way for a whiteboard service to be registered in a Http Context of the Http Service. For example adding a servlet filter for all servlets managed by the Http Service is not possible.

    Felix实现不共享HttpContext和ServletContextHelper:为了使用过滤器,必须在Http白板上注册servlet