Retrieve all users and their roles from LDAP using Java -
i have web application. ldap using apache directive studio. want users , roles in application.
i able particular information using following code.
import java.util.properties; import javax.naming.context; import javax.naming.namingexception; import javax.naming.directory.attributes; import javax.naming.directory.dircontext; import javax.naming.directory.initialdircontext; public class directorysample { public directorysample() { } public void dolookup() { properties properties = new properties(); properties.put(context.initial_context_factory, "com.sun.jndi.ldap.ldapctxfactory"); properties.put(context.provider_url, "ldap://localhost:10389"); try { dircontext context = new initialdircontext(properties); attributes attrs = context.getattributes("dc=example,dc=com"); system.out.println("all data: " + attrs.tostring()); } catch (namingexception e) { e.printstacktrace(); } } public static void main(string[] args) { directorysample sample = new directorysample(); sample.dolookup(); } }
want show users , roles list, need change query or else in advance.
you can use org.apache.directory.ldap.client.api.ldapconnection easy search.
once bind connection, search on connection. loop through cursor object want. first parameter should match dn of users parent. below example give idea.
entrycursor cursor = connection.search( "ou=users, dc=example, dc=com", "(objectclass=*)", searchscope.onelevel, "*" ); while ( cursor.next() ) { entry entry = cursor.get(); //play entry }
Comments
Post a Comment