Java 来自CAS的LDAP用户属性

Java 来自CAS的LDAP用户属性,java,spring-security,ldap,single-sign-on,cas,Java,Spring Security,Ldap,Single Sign On,Cas,您好,我在我的应用程序(spring应用程序)中使用CAS进行单点登录。 我可以使用CAS登录,我只获得用户名,但没有从CAS获得电子邮件或任何其他属性 对于CAS端的身份验证,我使用LDAP,并在deployerConfigContext.xml中配置了以下代码 public class RestAuthenticationUserDetailsService implements AuthenticationUserDetailsService<CasAssertionAuth

您好,我在我的应用程序(spring应用程序)中使用CAS进行单点登录。 我可以使用CAS登录,我只获得用户名,但没有从CAS获得电子邮件或任何其他属性

对于CAS端的身份验证,我使用LDAP,并在deployerConfigContext.xml中配置了以下代码

    public class RestAuthenticationUserDetailsService implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {

    @Override
    public UserDetails loadUserDetails(CasAssertionAuthenticationToken token)
            throws UsernameNotFoundException {
        Object principal = token.getPrincipal();
        String username = token.getName();
        LOGGER.info(username);

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        return new User(username, "", authorities);
    }

}
在authenticationManager中添加了以下代码

<property name="credentialsToPrincipalResolvers">
<list>
     <bean class="org.jasig.cas.authentication.principal.CredentialsToLDAPAttributePrincipalResolver">
            <property name="credentialsToPrincipalResolver">
                <bean
                    class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver" />
            </property>
            <property name="filter" value="sAMAccountName=%u" />
            <property name="principalAttributeName" value="sAMAccountName" />
            <property name="searchBase" value="DC=test,DC=com" />
            <property name="contextSource" ref="LDAPcontextSource" />
            <property name="attributeRepository">
                <ref bean="attributeRepository" />
            </property>
        </bean>
</list>
</property>

并使用LdapPersonAttributeDao

 <bean id="attributeRepository"
        class="org.jasig.services.persondir.support.ldap.LdapPersonAttributeDao">
        <property name="baseDN" value="cn=test,ou=test,dc=test,dc=com" />
        <property name="contextSource" ref="LDAPcontextSource" />
        <property name="requireAllQueryAttributes" value="true" />
        <property name="queryAttributeMapping">
            <map>
                <entry key="username" value="sAMAccountName" />
            </map>
        </property>
        <property name="resultAttributeMapping">
            <map>
                <entry key="displayName" value="cn" />
                <entry key="mail" value="email" />
            </map>
        </property>
    </bean>

我读过一些帖子,发现在下面的配置中添加allowedAttributes属性就是配置

    <bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
        <property name="registeredServices">
            <list>
                <bean class="org.jasig.cas.services.RegisteredServiceImpl">
    <property name="id" value="0" />
    <property name="name" value="HTTP" />
    <property name="description" value="Only Allows HTTP Urls" />
    <property name="serviceId" value="http://**" />
    <property name="allowedAttributes">
        <list>
            <value>cn</value>
            <value>mail</value>
        </list>
    </property>
</bean>

cn
邮件
在我的应用程序端,我编写了获取用户名和电子邮件的类,下面是代码

    public class RestAuthenticationUserDetailsService implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {

    @Override
    public UserDetails loadUserDetails(CasAssertionAuthenticationToken token)
            throws UsernameNotFoundException {
        Object principal = token.getPrincipal();
        String username = token.getName();
        LOGGER.info(username);

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        return new User(username, "", authorities);
    }

}
公共类重新身份验证UserDetailsService实现身份验证UserDetailsService{
@凌驾
public UserDetails loadUserDetails(CAAssertionAuthenticationToken令牌)
抛出UsernameNotFoundException{
Object principal=token.getPrincipal();
字符串username=token.getName();
LOGGER.info(用户名);
收集权限=新建ArrayList();
返回新用户(用户名“”,权限);
}
}
我得到了用户名,但没有得到其他属性,如电子邮件。当我调试时,我看到主属性是空的

有人能帮我了解一下如何获取应用程序的属性吗
提前感谢。

我在deployerConfigContext.xml的配置中看到了。 1.在中,键是LDAP条目属性,值是主体的(值) 2.在中,您应该在主体中允许值(例如displayName和mail)

希望它能帮助你,加油