我正在尝试配置一个部署在Servicemix上的带有授权和身份验证的cxf soap webservice。
我按如下方式配置了LDAP身份验证模块:
<!-- Bean to allow the $[karaf.base] property to be correctly resolved -->
<ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]"/>
<jaas:config name="myRealm">
<jaas:module className="org.apache.karaf.jaas.modules.ldap.LDAPLoginModule" flags="required">
connection.url = ldap://srv-ldap:389
user.base.dn = ou=people,dc=intranet,dc=company,dc=com
user.filter = (uid=%u)
user.search.subtree = false
role.base.dn = ou=groups,dc=intranet,dc=company,dc=com
role.filter = (member:=uid=%u,ou=people,dc=intranet,dc=company,dc=com)
role.name.attribute = cn
role.search.subtree = true
authentication = simple
</jaas:module>
</jaas:config>
<service interface="org.apache.karaf.jaas.modules.BackingEngineFactory">
<bean class="org.apache.karaf.jaas.modules.properties.PropertiesBackingEngineFactory"/>
</service>
这是beans.xml文件
<jaxws:endpoint id="myService"
implementor="com.myorg.services.impl.MyServiceWSImpl"
address="/myService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
</map>
</constructor-arg>
</bean>
<ref bean="authenticationInterceptor" />
<ref bean="authorizationInterceptor" />
</jaxws:inInterceptors>
<jaxws:properties>
<entry key="ws-security.validate.token" value="false" />
</jaxws:properties>
</jaxws:endpoint>
<bean id="authenticationInterceptor"
class="org.apache.cxf.interceptor.security.JAASLoginInterceptor">
<property name="contextName" value="myRealm" />
</bean>
<bean id="authorizationInterceptor"
class="org.apache.cxf.interceptor.security.SecureAnnotationsInterceptor">
<property name="securedObject" ref="securedBean"/>
</bean>
最后,在我的WebService实现中,我用@RolesAllowed注释了一个方法。
@RolesAllowed("Role1")
public Department get(String name) throws IdMException {
return service.get(name);
}
身份验证拦截器正在检索用户,对其进行身份验证,并将组作为RolePrincipal实例进行检索。然后,在授权拦截器(SecureAnnotationsInterceptor)中,读取方法配置,expectedRoles为"Role1",但SimpleAuthorizingInterceptor.isUserInRole方法返回false。
我还没有找到任何尝试做大致相同操作的示例,我找到的少数信息来自CXF文档页面http://cxf.apache.org/docs/security.html#Security-Authorization
我一定错过了一些重要的东西,希望有人能帮我。提前感谢并致以亲切的问候。
发布于 2014-06-25 01:09:27
您的问题是因为Karaf的RolePricipal没有像预期的那样实现组。而不是它,它实现了Pricipal,因此CXF认为第一个角色名是用户名。这就是"SimpleAuthorizingInterceptor.isUserInRole方法返回false“的原因。
一种解决方案是等待CXF的修复版本(2.7.11和3.0.0)。如果不能更新到较新版本,那么一种奇怪的临时解决方案(简单的变通方法)是在LDAP和方法中为用户添加多个角色。
你可以在这里找到更多关于这个bug的信息:CXF-5603
https://stackoverflow.com/questions/20492259
复制相似问题