cxf-rs-ws授权卡拉夫

时间:2013-12-10 10:42:18

标签: cxf jax-ws apache-karaf apache-servicemix

我正在尝试配置一个带有授权和身份验证的cxf soap webservice,以便部署在Servicemix上。

我按如下方式配置了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

我必须遗漏一些重要的东西,希望有人可以帮助我。 在此先感谢并亲切的问候。

1 个答案:

答案 0 :(得分:1)

你的问题是因为Karaf的RolePricipal没有像CXF那样实现Group。而不是它,它实现了Pricipal,因此CXF认为第一个角色名称是用户名。这就是为什么&#34; SimpleAuthorizingInterceptor.isUserInRole方法返回false&#34;。

解决方案是等待固定版本的CXF(2.7.11和3.0.0)。 如果无法更新到更新的版本,那么奇怪的和临时的解决方案(简单的解决方法)是在LDAP和方法中向用户添加多个角色。

您可以在此处找到有关该错误的更多信息:CXF-5603