Connecting and Validating a LDAP with Java for Dummies

It is very common for many applications to have to authenticate its users (or login). At the

It is very common for many applications to have to authenticate its users (or login). At the same time, these users’ information is often stored in servers. In this particular case, we’ll see how to authenticate a user – with its password – and how to get data from users or groups to a Lightweight Directory Access Protocol (LDAP) server with Java.

How to get connected?

First, one must know which libraries have to be imported, or if any of these libraries must be installed. To use the LDAP server, only the following libraries are needed:

To connect to the server, one must load into a Hashtable all the fields we want to send (URL for the connection, user credentials, authentication type, and others that may be necessary), for example:

In case of not being able to connect to the server with user credentials, an exception will appear (NamingException).

Once connected to the server, we can get the context that allows us to make inquiries, information, authenticate, etc. To log off, one must simply type:

Authenticating a user (with its password)

In order to do so, all one must do is log oneself in a similar manner to that discussed in the previous example, but indicating the corresponding username and password. Note that it is necessary to write the full path (for example, if the username is “john”, it is not enough to simply write as a user “john”), for example: “CN=nombre_usuario,OU=Unidad,DC=dominio”.

If you are able to get connected, you can obtain the context, and if you do not have the correct username or password, the same exception as before will appear:

Obtaining a user’s or group’s attributes

To do so, one must first get the connection context as indicated above (either as an administrator or common user, but it may depend on the LDAP server).

Then, we need to indicate how many and what are the names of the attributes we want (which could, potentially, be none, if we just want to check whether a user who meets the conditions exists), and the filtering conditions (which are always important although they might be random).

First, we include the following libraries:

For example, we obtain the attributes “field1 ” and ” field2 ” of a given user:

The variants that can be sought are enormous, therefore, we propose a more general example of how to perform a search:

Example of use:


It is very easy to authenticate and inquire information from a LDAP server in Java, and the code can be relatively simple and easy to understand (although as the query becomes more complex, so does the code).

On the downside, we can see that it is necessary to indicate where we want the query to inquire the information (knowing the whole structure of LDAP), but more importantly, it must be given in the correct order; otherwise the query will fail.

Are you searching for the best technical solution for your company? Do not hesitate to Contact us for more information. ¡We will get back to you!

Share this articleShare on LinkedInTweet about this on TwitterShare on FacebookShare on Google+Email this to someone
Go Back