我在使用Pax Web白板服务向通过CXF注册的正在运行的JaxRS端点注册javax.servlet.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名称和urlpattern,甚至尝试了urlpattern /*,然后尝试了一种略有不同的方法,从蓝图中删除服务声明,并通过蓝图的初始化方法直接添加过滤器:
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);
}正如日志语句所反映的那样,该方法确实被调用了,但过滤器仍未执行。
那么,我是不是完全错了呢?我的“应用程序”实际上只是一个注册到CXF servlet的端点。这部分工作正常,我可以调用其中定义的REST服务。但是无论我做什么,过滤器都不会执行。我正在使用一些我不太了解的库(Servlet/Filters、Pax-Web和Writeboard扩展程序)。我不知道这到底为什么不起作用吗?我的猜测是,每个包都有不同的httpcontext,我不能简单地为我自己的测试包中的另一个包(CXF)注册一个过滤器。
如果这是真的,有人能告诉我如何正确地解决这个问题吗?我可以获取CXF捆绑包bundlecontext并注册过滤器,但这似乎是一个可怕的黑客攻击。如果不是这样,有人能告诉我为什么这不起作用吗?
发布于 2016-03-22 05:03:20
你说得对,每个捆绑包都应该有自己的httpContext。有了Pax-Web,共享httpContextes成为可能。为此,您需要为最初注册httpContext的捆绑包启用它。不过,在本例中,cxf包负责处理它。由于共享上下文是一个仅限pax-web的功能(直到实现了OSGi R6的v6为止),这不会被添加到cxf中,因为cxf倾向于依赖最小的可能解决方案,即HttpService。
基本上,即使可以与不同的捆绑包共享httpContextes,但在这种情况下对您来说也是不可能的。
发布于 2016-03-26 17:01:35
我建议使用JAAS而不是shiro作为登录和存储身份验证信息的方法。然后,您可以在shiro以及其他安全实现中使用它来进行授权。
对于CXF,有一个JAASLoginFeature。它可以接收基本身份验证和UserNameToken。请参阅https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=42568988
这也具有与标准karaf身份验证相同的工作方式的优点。因此,默认情况下,您可以在etc/users.properties中定义用户和组,但也可以将karaf附加到ldap。
如果您使用blueprint,那么您可以使用blueprint-authz使用注释进行基于角色的授权。请参阅https://github.com/apache/aries/tree/trunk/blueprint/blueprint-authz和https://github.com/apache/aries/blob/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/authz/testbundle/impl/SecuredServiceImpl.java
https://stackoverflow.com/questions/36129030
复制相似问题