I had written a blog post about Querying Active Directory using C# it’s simple and easy to understand then I thought to provide similar approach/article Querying Active Directory using Java. This article is all about how to achieve Querying Active Directory using Java.
I will take you through below elements in detail-
- Retrieve User Details or an Object from AD based on Username – sAMAccountName
- Retrieve User Details or an Object from AD based on user Email ID – mail
To know more about filters, attributes/properties you can use for active directory query.
Required Details
- LDAP address (For e.g.: myjeeva.com or IP of the Domain Controller/Global Catalog[GC])
- Port # (For e.g.: 3289 or 389) where would you to like search user details?
- Domain Username
- Domain Password
How to do – Step by Step explaination
For an easy understanding perspective; I will be following line by line approach. ActiveDirectory Class file and example of how to use that ActiveDirectory class file in java program. Downloads of these files you will find below.
Step 1
Compose LDAP address and supply following parameters username, password, ldap address as a domain into ActiveDirectory constructor.
ActiveDirectory activeDirectory = new ActiveDirectory(username, password, domain);
Step 2
Invoke searchUser method with parameters of searchTerm, choice and searchBase.
NamingEnumeration<SearchResult> result = activeDirectory.searchUser(searchTerm, choice, “DC=myjeeva,DC=com”);
Step 3
Now you have your search result in result variable.
How it works?
Part 1
ActiveDirectory constructor-
- It creates properties instance with given values (ldap address, username, password)
- It initializes the Directory Context
- It assign the Search Scope and return attribute names
/** * constructor with parameter for initializing a LDAP context * * @param username a {@link java.lang.String} object - username to establish a LDAP connection * @param password a {@link java.lang.String} object - password to establish a LDAP connection * @param domainController a {@link java.lang.String} object - domain controller name for LDAP connection */ public ActiveDirectory(String username, String password, String domainController) { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); properties.put(Context.PROVIDER_URL, "LDAP://" + domainController); properties.put(Context.SECURITY_PRINCIPAL, username + "@" + domainController); properties.put(Context.SECURITY_CREDENTIALS, password); // initializing active directory LDAP connection try { dirContext = new InitialDirContext(properties); } catch (NamingException e) { LOG.severe(e.getMessage()); } // default domain base for search domainBase = getDomainBase(domainController); // initializing search controls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); searchCtls.setReturningAttributes(returnAttributes); }
Part 2
searchUser method utilizes the filter method to construct the active directory query.
/** * search the Active directory by username/email id for given search base * * @param searchValue a {@link java.lang.String} object - search value used for AD search for eg. username or email * @param searchBy a {@link java.lang.String} object - scope of search by username or by email id * @param searchBase a {@link java.lang.String} object - search base value for scope tree for eg. DC=myjeeva,DC=com * @return search result a {@link javax.naming.NamingEnumeration} object - active directory search result * @throws NamingException */ public NamingEnumeration<SearchResult> searchUser(String searchValue, String searchBy, String searchBase) throws NamingException { String filter = getFilter(searchValue, searchBy); // For eg.: "DC=myjeeva,DC=com"; String base = (null == searchBase) ? domainBase : getDomainBase(searchBase); return this.dirContext.search(base, filter, this.searchCtls); } private String getFilter(String searchValue, String searchBy) { String filter = this.baseFilter; if(searchBy.equals("email")) { filter += "(mail=" + searchValue + "))"; } else if(searchBy.equals("username")) { filter += "(samaccountname=" + searchValue + "))"; } return filter; }
Downloads
ActiveDirectory.java SampleUsageActiveDirectory.java
Completion
That’s it, you have learned querying active directory using java and you can download artifacts. Try it out yourself with class provided and experiment it.
For any queries please leave a comment!