List All Users from LDAP using JNDI

We  have already seen the fundamentals of JNDI  along with examples for Naming as well as Directory Services using JNDI. So far, all the  JNDI examples using LDAP were fetching/updating a particular user from the DIT. In this section we would list all the users from LDAP using JNDI in “ou=users,ou=system” in a generic way.

List All Users from LDAP using JNDI

The basic setup is same as that of the previous examples.We are using Apache Directory Studio as the LDAP provider .In this case we are searching the users details using the search() method of DirContext object.

ldap

DirectorySample.java

 

The code shown here lists all the attributes of all users.This can be controlled by adding returning attributes to search controls .

Example:

String[] attributes = { “cn”, “employeeNumber”, “sn” };
searchCtrls.setReturningAttributes(attributes);

Continue reading →