无法将过滤器注册到我的捆绑包中的CXF捆绑包

时间:2016-03-21 10:49:24

标签: servlets osgi pax-web

我无法使用Pax Web Whiteboard服务将javax.servlet.Filter注册到通过CXF注册的正在运行的JaxRS端点。我尝试了一些不同的方法,即将Filter作为服务注册,并使用org.ops4j.pax.web.service.WebContainer直接注册过滤器。

对于我的测试,我启动了karaf,并安装了pax-web,webconsole,cxf和pax-web白板服务。 我注册了一个蓝图:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
     xmlns:cxf="http://cxf.apache.org/blueprint/core"
     xsi:schemaLocation="
     http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
     http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
     http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
                 ">

     <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus>

     <jaxrs:server id="webfiltersample" address="/webfilter">
        <jaxrs:serviceBeans>
           <ref component-id="serviceBean" />
        </jaxrs:serviceBeans>
     </jaxrs:server>

     <service id="servletFilterService" interface="javax.servlet.Filter">
        <service-properties>
            <entry key="filter-name" value="BasicWebFilter"/>
            <entry key="urlPatterns" value="/*"/>
            <entry key="initParams" value=""/>
        </service-properties>
    <bean class="test.webfilter.BasicWebFilter"/>
</service>

    <bean id="serviceBean" class="test.webfilter.WebFilterSample" init-method="init" destroy-method="destroy">
    <property name="bundleContext" ref="blueprintBundleContext"/>
    </bean>
</blueprint>

但是,永远不会调用此过滤器。我已尝试使用servlet名称和urlpatterns,甚至尝试urlpattern / * 然后我尝试了一种稍微不同的方法,从蓝图中删除服务声明,并直接通过蓝图的init方法添加过滤器:

public void init(){
    logger.info("Starting Sample");
    filter = new WebFilter();
    ServiceReference<WebContainer> ref = bundleContext.getServiceReference(BasicWebFilter.class);
    WebContainer container = bundleContext.getService(ref);
    logger.info("Registering "+ filter + " with "+ container);
    container.registerFilter(filter, new String[]{"/cxf/*"}, new String[]{""}, null, null);
    bundleContext.ungetService(ref);
    }

确实调用了该方法,正如日志语句所反映的那样,但仍未执行过滤器。

我对这是如何工作完全错了?我的应用程序&#34;实际上只是注册到CXF servlet的端点。这部分工作正常,我可以调用其中定义的REST服务。但无论我做什么,过滤器都没有执行。我正在使用一些库,我不太了解(Servlets /过滤器,Pax-Web和Writeboard扩展器)我不知道为什么这不起作用?我的猜测是每个bundle都有不同的httpcontexts,我不能在我自己的测试包中为另一个bundle(CXF)注册一个过滤器。

如果这是真的,有人可以告诉我如何正确解决这个问题吗?我可以获得CXF捆绑bundlecontext并注册过滤器,但这似乎是一个可怕的可怕黑客。 如果不是这样,有人可以告诉我为什么这不起作用吗?

2 个答案:

答案 0 :(得分:1)

你是对的,每个包都应该拥有它自己的httpContext。使用Pax-Web,可以共享httpContextes。为此,您需要为最初注册httpContext的bundle启用它。虽然在这种情况下,它是cxf包,它会照顾它。由于共享上下文是仅限pax-web的功能(直到实现OSGi R6的v6),因此cxf不会添加到cxf,因为cxf倾向于依赖最小的可能解决方案,即HttpService。 基本上即使可以与不同的bundle共享httpContextes,但这种情况对你来说是不可能的。

答案 1 :(得分:0)

我建议使用JAAS而不是shiro作为登录和存储身份验证信息的方法。然后,您可以在shiro以及其他安全实现中使用它来执行授权。

对于CXF,有一个JAASLoginFeature。它可以接收基本身份验证以及UserNameToken。见https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=42568988

这也具有与标准karaf认证相同的优点。因此,默认情况下,您可以在etc / users.properties中定义用户和组,但您也可以将karaf附加到ldap。

如果您使用蓝图,则可以使用注释使用blueprint-authz do role based authorization。请参阅https://github.com/apache/aries/tree/trunk/blueprint/blueprint-authzhttps://github.com/apache/aries/blob/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/authz/testbundle/impl/SecuredServiceImpl.java