使用Java获取LDAP域用户名列表

时间:2011-12-13 01:03:42

标签: java ldap spring-ldap adldap

ldap用户名需要在输入框中显示为自动完成功能。我想获得如下用户列表:

        String ldapURL = "ldap://192.26.75.5:389/dc=northamerica,dc=company,dc=com";
    String principalPrefix = "domainName";      
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();

    Hashtable<String, String>environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL,ldapURL);
    environment.put(Context.SECURITY_AUTHENTICATION,"simple");
    environment.put(Context.SECURITY_PRINCIPAL,principalPrefix + "\\" + username);
    environment.put(Context.SECURITY_CREDENTIALS,password);
    environment.put( Context.REFERRAL, "follow" );

    DirContext context = null;
    NamingEnumeration<SearchResult> enumResult = null;      
    try
    {
                    context = new InitialDirContext(environment);                       
                    SearchControls controls = new SearchControls();                     
                    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    String[] attrIDs ={"ou","uid", "givenname", "sn", "mail"};
                    controls.setReturningAttributes(attrIDs);
                    enumResult = context.search("","(&(objectCategory=person)(objectClass=user)(CN=*))", controls);                     
                    if(enumResult != null)
                    {
                                    //authentication successful                                 
                    }                       
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }

然而,“enumResult”总是获得单个用户值。如果我错过了某些内容或者错误的方法,请告诉我。 任何帮助/建议/建议将不胜感激!感谢。

0 个答案:

没有答案