java在Karaf中的JAXRS包上使用Spring安全性
我有一个OSGi包,它使用JAX-RS处理一些REST服务。此捆绑包使用ApacheCXF在Karaf中运行。我需要对某些路径/方法组合应用基本http身份验证。我一直在修补Spring安全性,新的3.1版本似乎可以让您完全做到这一点,但是我在OSGi中遇到了很多麻烦
作为测试,我创建了一个非常简单的bean。xml文件:
<beans>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-http.xml"/>
<import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml"/>
<jaxrs:server id="serverTest" address="/test">
<jaxrs:serviceBeans>
<ref bean="tstSvr"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="tstSvr" class="com.demo.Test"/>
<security:http auto-config="true">
<security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" method="PUT" />
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="pass" authorities="ROLE_USER"/>
<security:user name="admin" password="pass" authorities="ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
现在,有趣的部分来了。。。从我一直在做的所有阅读中,我需要一个网站。xml以使任何一种方法都有效。例如我尝试使用的示例:
<web-app>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
使用这两个文件的组合不起作用。我说的“没用”是指什么都没发生。没有错误消息,没有异常,捆绑包的功能与我尝试添加spring安全性之前一样。我假设问题是捆绑包需要是一个WAR或WAB,用于web。要加载的xml。这是正确的吗
更重要的是,有没有一种方法可以让spring在没有web的情况下工作。xml
我的工作假设是,我需要将该捆绑包作为一个捆绑包,以便CXF加载它,因此我无法将其转换为WAR或WAB,但我不能完全确定情况是否如此
谢谢你能提供的任何帮助
更新:
在做了大量额外的谷歌搜索之后,我发现了一个forum post,其中提到将Web-FilterMappings: springSecurityFilterChain;url-patterns:="/*"
添加到您的清单中,而不是使用web。xml。但是,似乎仍然需要使用WAB而不是普通捆绑包。为了以防万一,我已经在我的清单中添加了这一行,但它没有效果。我的问题似乎变成了:如何将WAB与CXF结合使用
更新2: 因为这个问题不够长。。。我决定尝试使用Spring安全注释,而不是截取url,只是为了看看会发生什么。当我尝试进入一个安全路径时,我会得到一个有趣的堆栈跟踪:
Caused by: org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:323)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:196)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[46:org.springframework.aop:3.0.5.RELEASE]
spring网站表示,这种情况会在您第一次尝试匿名连接到安全服务时发生,不会再发生。我每次都是这样。从异常情况来看,我在清单中的条目似乎被拾取了,可能问题与我想象的不同。有人知道为什么会这样吗?我在豆子里遗漏了一些条目。xml使基本http身份验证工作
# 1 楼答案
首先,您需要清楚地了解spring和web是什么。xml可以
如果您想使用servlet进行Http而不使用容器,我想您必须注册一个HttpService,请参见:http://karaf.apache.org/manual/latest-2.2.x/users-guide/http.html
发生异常是因为当它到达映射方法时,SecurityContext中没有身份验证bean。您尚未在映射中定义任何匿名访问,这可能就是它失败的原因
基本上,可以在http配置中定义一个匿名AuthenticationFilter,或者使用permitAll定义一个URL模式。否则,当您第一次尝试以匿名方式连接同一会话时,您将继续收到该异常