Java – Connect Windows Active Directory Through LDAP @ 3

This time, we would like to gather all users account info. This time we need to check return objects recursively until it is in the Person category.

RetrieveAllUsersAttributes.java

import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;

public class RetrieveAllUsersAttributes {
	
	public static void main(String[] args) {
		try {
			RetrieveAllUsersAttributes retrieveAllUsersAttributes = new RetrieveAllUsersAttributes();
			retrieveAllUsersAttributes.listSubContext(retrieveAllUsersAttributes.getLdapContext(), "DC=cecid03server,DC=hku,DC=hk");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public LdapContext getLdapContext(){
		LdapContext ctx = null;
		try{
			Hashtable env = new Hashtable();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
			env.put(Context.SECURITY_AUTHENTICATION, "Simple");
			env.put(Context.SECURITY_PRINCIPAL, "administrator@cecid03server.hku.hk");
			env.put(Context.SECURITY_CREDENTIALS, "your password here");
			env.put(Context.PROVIDER_URL, "ldap://cecid-03server:389");
			ctx = new InitialLdapContext(env, null);
			System.out.println("Connection Successful.");
		}catch(NamingException nex){
			System.out.println("LDAP Connection: FAILED");
			nex.printStackTrace();
		}
		return ctx;
	}
	
	public void listSubContext(DirContext ctx, String rootContext) throws NamingException {
		String[] attributeNames = { "memberOf", "mail", "name", "sAMAccountName" };
		NamingEnumeration contentsEnum = ctx.list(rootContext);
		while (contentsEnum.hasMoreElements()) {
			NameClassPair ncp = (NameClassPair) contentsEnum.next();
			String userName = ncp.getName();
			Attributes attr1 = ctx.getAttributes(userName + "," + rootContext, new String[] { "objectcategory" });
			if (attr1.get("objectcategory").toString().indexOf("CN=Person") == -1) {
				// Recurse sub-contexts
				listSubContext(ctx, userName + "," + rootContext);
			} else {
				Attributes attrs = ctx.getAttributes(userName + "," + rootContext, attributeNames);
				Attribute groupsAttribute = attrs.get("memberOf");
				Attribute mailAttribute = attrs.get("mail");
				Attribute nameAttribute = attrs.get("name");
				Attribute accountAttribute = attrs.get("sAMAccountName");
				if (accountAttribute != null) {
					for (int i = 0; i < accountAttribute.size(); i++) {
						System.out.println((String)accountAttribute.get(i));
					}
				}
				if (groupsAttribute != null) {
					for (int i = 0; i < groupsAttribute.size(); i++) {
						System.out.println((String)groupsAttribute.get(i));
					}
				}
				if (mailAttribute != null) {
					for (int i = 0; i < mailAttribute.size(); i++) {
						System.out.println((String)mailAttribute.get(i));
					}
				}
				if (nameAttribute != null) {
					for (int i = 0; i < nameAttribute.size(); i++) {
						System.out.println((String)nameAttribute.get(i));
					}
				}
			}
		}
	}
}

 

Done =)

Reference:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.