简体   繁体   中英

Java: LDAP Search returning 1 row

I am trying to get all users from my active directory however my code is returning just one row. I have tried the below which is currently only outputting one user.

private void getUserBasicAttributes(String username, LdapContext ctx) {

    try {
        List<String> usersList = new ArrayList<String>();
        SearchControls constraints = new SearchControls();
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);


        //First input parameter is search bas, it can be "CN=Users,DC=YourDomain,DC=com"
        //Second Attribute can be uid=username
         NamingEnumeration<SearchResult>  answer = ctx.search("DC=domain,DC=com", "(&(objectCategory=user))"
             , constraints);
        if (answer.hasMoreElements()) {
        Person person = new Person();
           SearchResult  attrs = ((SearchResult) answer.next());
            String names[] = attrs.getName().split(",");
                 String name[] = names[0].split("=");

            usersList.add(name[1]);


        }else{
            throw new Exception("Invalid User");
        }

        System.out.println(usersList.size());

    } catch (Exception ex) {
        ex.printStackTrace();
    }


}

You are not looping over all the results, add a while loop inside the if

if (answer.hasMoreElements()) {
    while(answer.hasMoreElements()) {
        Person person = new Person();
        SearchResult  attrs = ((SearchResult) answer.next());
        String names[] = attrs.getName().split(",");
        String name[] = names[0].split("=");

        usersList.add(name[1]);
    }
}else{
    throw new Exception("Invalid User");
}

You need while instead of if :

while (answer.hasMoreElements()) {
    Person person = new Person();
    SearchResult  attrs = ((SearchResult) answer.next());
    String names[] = attrs.getName().split(",");
    String name[] = names[0].split("=");
    usersList.add(name[1]);
}
if (usersList.size() == 0) {
    throw new Exception("Invalid User");
}

You can simplify the name-element handling as well. No need to parse the DN. Just specify the attribute(s) you want returned up front and retrieve them directly.

You are making this too hard. No reason to perform any "splitting" pf values.

// Specify the ids of the attributes to return
String[] attrIDs = { "uid" };

// Get ONLY the attributes desired
Attributes answer = ctx.getAttributes("CN=Users,DC=YourDomain,DC=com", attrIDs);
for (NamingEnumeration ae = answer.getAll(); ae.hasMore();) {
    Attribute attr = (Attribute)ae.next();
    System.out.println("attribute: " + attr.getID());
    /* Print each value */
    for (NamingEnumeration e = attr.getAll(); e.hasMore();
         System.out.println(e.next()))
        ;
}

Let me know how I can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM