2013-05-20 3 views
1

Аутентификация работает правильно. Я пытаюсь получить атрибуты пользователя LDAP, используя метод «поиска» LdapTemplate. Моя весна-security.xml:Весна безопасности. Как получить атрибуты пользователя LDAP?

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/security 
         http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 
... 
    <ldap-server url="ldap://ldap.andri.com:389/dc=andri,dc=com" /> 

    <authentication-manager> 
     <authentication-provider ref='jtwAuthProvider' /> 
     <ldap-authentication-provider 
      group-search-filter="member={0}" user-search-base="ou=Addressbook" 
      user-search-filter="uid={0}" /> 
    </authentication-manager> 

    <beans:bean id="jtwAuthProvider" 
     class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> 
     <beans:constructor-arg> 
      <beans:bean 
       class="org.springframework.security.ldap.authentication.BindAuthenticator"> 
       <beans:constructor-arg ref="contextSource" /> 
       <beans:property name="userSearch"> 
        <beans:bean id="userSearch" 
         class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> 
         <beans:constructor-arg index="0" 
          value="ou=Addressbook,dc=ldap,dc=andri,dc=com" /> 
         <beans:constructor-arg index="1" 
          value="userPrincipalName={0}" /> 
         <beans:constructor-arg index="2" 
          ref="contextSource" /> 
        </beans:bean> 
       </beans:property> 
      </beans:bean> 
     </beans:constructor-arg> 
    </beans:bean> 

    <beans:bean id="contextSource" 
     class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
     <beans:constructor-arg value="ldap://ldap.andri.com:389" /> 
     <beans:property name="userDn" 
      value="cn=LDAPaccess,ou=Special,dc=ldap,dc=andri,dc=com" /> 
     <beans:property name="password" value="GfGTgFD" /> 
    </beans:bean> 

</beans:beans> 

Войти контроллер:

... 
LdapTemplate template; 

@Autowired 
public void setTemplate(LdapContextSource contextSource) { 
    template = new LdapTemplate(contextSource); 
} 
... 
@SuppressWarnings("unchecked") 
@RequestMapping(value = "/books", method = RequestMethod.GET) 
public String books(ModelMap model, Principal principal) 
     throws BookServiceException { 

    class UserAttributesMapper implements AttributesMapper { 

     @Override 
     public Object mapFromAttributes(Attributes attributes) 
       throws NamingException { 
      Map<String, String> map = new HashMap<String, String>(); 
      String fullname = (String) attributes.get("displayName").get(); 
      String email = (String) attributes.get("mail").get(); 
      String title = (String) attributes.get("title").get(); 

      map.put("fullname", fullname); 
      map.put("email", email); 
      map.put("title", title); 
      return map; 
     } 
    } 

    Map<String, String> results = new HashMap<String, String>(); 
    String objectClass = "samAccountName=" + principal.getName(); 

    LinkedList<Map<String, String>> list = (LinkedList<Map<String, String>>) template 
      .search("ou=Addressbook,dc=andri,dc=com", objectClass, 
        new UserAttributesMapper()); 
    results = list.get(0); 
    model.addAttribute("userinfo", results.toString()); 
    return "books"; 
} 

Однако я получаю сообщение об ошибке: org.springframework.ldap.InvalidNameException: [LDAP: код ошибки 34 - недействительный DN];

Я попытался с помощью другого DN: "НУ = Addressbook, dc = Андри, DC = ком" "сп = aartemenko, НУ = Addressbook, dc = Андри, DC = ком" «dc = Андри, dc = ком» "" "сп = aartemenko, НУ = Special, dc = Андри, DC = ком" т.д.

Но результат тот же. Что я делаю неправильно?

ответ

0

Извините, это действительно глупо. Это ошибка в LDAPaccess cn.

Смежные вопросы