使用LDAP和自定义UserDetailsContextMapper的Spring Security

nvr*_*vrs 5 ldap spring-security

我正在尝试使Spring Security 3.05与修改后的UserDetailsContextMapper一起工作,以便我可以从他们需要的方式获取更多信息,这项任务似乎相当简单,但没有成功.

我已将Spring Security配置为使用以下bean的LDAP身份验证:

<bean id="contextSource"
    class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <constructor-arg value="ldaps://192.168.1.102:636" />
    <property name="userDn" value="manager" />
    <property name="password" value="password" />
</bean>

<bean id="ldapAuthProvider"
    class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
    <constructor-arg>
        <bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
            <constructor-arg ref="contextSource" />
            <property name="userSearch">
                <bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
                    <constructor-arg index="0" value="" />
                    <constructor-arg index="1" value="(mail={0})" />
                    <constructor-arg index="2" ref="contextSource" />
                </bean> 
            </property>
        </bean>
    </constructor-arg>
    <property name="userDetailsContextMapper" ref="myContextMapper" />
</bean>
Run Code Online (Sandbox Code Playgroud)

然而即使我已经定义myContextMapper为:

<bean id="myContextMapper" class="com.mypackage.MyLDAPUserDetailsMapper">
    <property name="rolePrefix" value="TEST_PREFIX" />
</bean>
Run Code Online (Sandbox Code Playgroud)

这是行不通的.意味着忽略了自定义映射器(我没有得到任何调试输出).

ps applicationContext-security.xml可以在下面看到,除了被忽略的自定义UserDetailsMapper,身份验证和角色分配工作正常.

<authentication-manager>
    <ldap-authentication-provider server-ref="contextSource"/>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

Rit*_*esh 9

您不需要配置内置的UserDetailsContextMapper类.Spring Security UserDetailsContextMapper根据所LdapUserDetails请求的类的类型自动选择正确的,这是由user-details-class属性配置的ldap-authentication-provider.如果您使用自己的上下文映射器,则使用该属性对其进行配置user-context-mapper-ref.