2015-06-09 33 views
1

您好,我在我的應用程序中使用CAS進行單點登錄(Spring應用程序)。 我能夠用CAS登錄,我只獲取用戶名,而不是電子郵件或來自CAS的任何其他屬性。下面來自CAS的LDAP用戶屬性

對於CAS側我使用LDAP驗證和在deployerConfigContext.xml裏配置是加入下面的代碼

<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> 
代碼

在的AuthenticationManager

我已閱讀一些帖子,並發現在下面的配置中添加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> 

在我的應用程序方面我已經寫類來獲取用戶名和電子郵件以下是代碼

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); 
    } 

} 

我得到的用戶名,但沒有得到其他屬性,如電子郵件。當我在調試時,我看到的主要屬性是空的。

有人可以幫助我如何獲得屬性到我的應用程序 在此先感謝。

回答

1

我在配置中看到deployerConfigContext.xml。 1.在,關鍵是LDAP條目屬性,價值是校長(值) 2.在,你應該允許在主(例如顯示名&郵件)

希望它可以幫助你值,歡呼!