简体   繁体   中英

LDAP: Fetching the list containing info of all users in directory using Java

I have a requirement to fetch the list containing information of all the users present in the directory. And I'm trying to fetch by the following piece of code:

        DirContext ctx = new InitialDirContext(env);

        boolean ignoreCase = true;
        Attributes matchAttrs = new BasicAttributes(ignoreCase);
        matchAttrs.put( new BasicAttribute("") );

        //LDAP_Attributes : Attributes of every user e.g. name, phone# etc.
        NamingEnumeration answer = ctx.search( "ou=People", matchAttrs, LDAPUser.LDAP_ATTRIBUTES );

I don't have the access to LDAP server owing to security constraints and hence, unable to test the same. Kindly suggest if the above approach is correct. Thanks!

check the below code sample to get the information using Search(). i think it might help you.

AllSearch.java

package usingj2ee.naming;

import javax.naming.*;
import javax.naming.directory.*;

public class AllSearch
{
    public static void main(String[] args)
    {
        try
        {
// Get the initial context
            InitialDirContext ctx = new InitialDirContext();

            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Search for items with the specified attribute starting
// at the top of the search tree
            NamingEnumeration objs = ctx.search(
                "ldap://ldap.wutka.com/o=Wutka Consulting, dc=wutka, dc=com",
                "(objectClass=*)", searchControls);

// Loop through the objects returned in the search
            while (objs.hasMoreElements())
            {
// Each item is a SearchResult object
                SearchResult match = (SearchResult) objs.nextElement();

// Print out the node name
                System.out.println("Found "+match.getName()+":");

// Get the node's attributes
                Attributes attrs = match.getAttributes();

                NamingEnumeration e = attrs.getAll();

// Loop through the attributes
                while (e.hasMoreElements())
                {
// Get the next attribute
                    Attribute attr = (Attribute) e.nextElement();

// Print out the attribute's value(s)
                    System.out.print(attr.getID()+" = ");
                    for (int i=0; i < attr.size(); i++)
                    {
                        if (i > 0) System.out.print(", ");
                        System.out.print(attr.get(i));
                    }
                    System.out.println();
                }
                System.out.println("---------------------------------------");
            }
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}

Referenced from here

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