Extract all users and their roles from LDAP using Java

I have a web application. For LDAP, I use Apache Directive Studio. I want all users and their roles to be in my application.

I can get specific information using the 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();
        }

    }

enter image description here
I want to show a list of all users and roles, so I need to change the request or something else Thanks in Advance.

+4
source share
1 answer

You can use org.apache.directory.ldap.client.api.LdapConnection for a convenient search.

, . , . DN . , .

EntryCursor cursor = connection.search( "ou=users, dc=example, dc=com", "(objectclass=*)", SearchScope.ONELEVEL, "*" );

    while ( cursor.next() )
    {
        Entry entry = cursor.get();
            //play with the entry
    }
+1

Source: https://habr.com/ru/post/1525562/


All Articles