Querying Active Directory using Java

Querying Active Directory using JavaI 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

Important Reference: will introduce you to the classes needed for querying Active Directory using Java. Have a look and know more about it.


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.

Step 2

Invoke searchUser method with parameters of searchTerm, choice and searchBase.

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

Part 2

searchUser method utilizes the filter method to construct the active directory query.


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!