Simple Way to Configure LDAP Authentication using Spring Boot – Source Code on GitHub

LDAP authentication is one of the widely used approaches in enterprise-grade applications.  LDAP is used as a central repository for user information and applications will connect to this repository for user searches and authentication.

Configure LDAP Authentication using Spring Boot:

In this article, we will see how to do LDAP authentication using Spring Boot. Along with Spring Boot, we are using an online free LDAP  test server setup for user information. We will use the information provided by them to configure a connection in our project.

Software used in this example

  • Spring Boot 1.5.7.RELEASE
  • Java 8
  • Maven
  • Eclipse

Demo Project Structure

As we are using a web application along with usual spring boot dependency we need to add dependencies for ldap and thymeleaf for template system. The entries in pom will be like

For LDAP connection we need set few parameters like server url, port, principal user, password, base domain name. This information you can get from your LDAP or Active Directory team. In our case we are using sample online server and they have made this information available for us. We will keep these as properties in our application properties file.

If you connect to the sample server from any of your LDAP Browser, you will see the directory structure like below 

From above image you can check how we came down to base domain name, user pattern etc. Once we have these properties set we will add a security config bean to our project which will configure a Ldap connection using these properties.

From above you can see that we have configured all urls under /profiles as secured. So if you are not authenticated and try to access the url you will be presented with a login form.

Also you can see that we have configured ldap authentication using spring boot based on a condition. If our properties file have a property ldap.enabled set to true then only the ldap configuration is triggered or else it will fall back to basic in-memory authentication.

Regarding html pages, we have simply mapped /login, /profile urls to respective thymeleaf  templates

Thats it on configuration front. Now we will run our application and access http://localhost:8999/profile .  You will be redirected to login page as

Now try and put any of the users from below. All users have a password as password

  • riemann
  • gauss
  • euler
  • euclid

If you enter correct user/password you will be taken to profile page else it will show you login error.

You can download the code from our Github.

Download Code
30 Comments
  1. Anar
    December 18, 2021 | Reply
  2. Sushmita Goswami
    August 17, 2020 | Reply
  3. January 9, 2020 | Reply
  4. January 9, 2020 | Reply
  5. Mamatha
    January 8, 2020 | Reply
  6. Rajesh
    November 19, 2019 | Reply
  7. July 25, 2019 | Reply
    • Pavan
      July 27, 2019 | Reply
  8. January 22, 2019 | Reply
  9. Ajay Singh
    August 22, 2018 | Reply
  10. SIVA THEJA PAKANATI
    August 21, 2018 | Reply
  11. Amr El-Deeb
    June 22, 2018 | Reply
    • Pavan
      June 22, 2018 | Reply
  12. Ijoe
    June 16, 2018 | Reply
    • Pavan
      June 18, 2018 | Reply
      • Amr El-Deeb
        June 22, 2018 | Reply
  13. Vandana
    April 2, 2018 | Reply
    • Pavan
      April 2, 2018 | Reply
      • Pravat
        June 12, 2018 | Reply
        • Pavan
          June 14, 2018 | Reply
          • Pravat
            June 17, 2018 |
          • Pavan
            June 18, 2018 |
  14. March 12, 2018 | Reply
    • Pavan
      March 13, 2018 | Reply
      • Dev52
        March 11, 2019 | Reply
  15. PannnHo
    January 18, 2018 | Reply
  16. Izet
    December 25, 2017 | Reply
    • Pavan
      December 26, 2017 | Reply
      • raghu
        June 22, 2018 | Reply
        • Pavan
          June 22, 2018 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *