top of page

Authentication With LDAP in Spring

Updated: Nov 29, 2021

Referring to the Spring Guide to Spring security for authenticating a user with LDAP, this blog is to create the working example for someone to try it out.

The example present as part of the guide is having some configuration missing, without which the example won't work. I got to know from some Stackoverflow pages about the missing configurations and added the correct form to this post.


I am using Maven for my project and it requires following external libraries for enabling the LDAP authentication in our spring project.


Web Security Configurer Adapter

In order to get the LDAP authentication working, we have to add another configuration which will extend WebSecurityConfigurerAdapter class. The configure() method will be overridden to manually add some additional configuration for the login works in your spring project. The WebSecurityConfigurerAdapter class implements WebSecurityConfigurer interface.

It also provides an embedded LDAP server with default configuration. We will use ldapauthentication() method which will configure things so that the username in the login form is plugged into {0} such that it searches below pattern in the LDAP server


In the flow, the configure method with AuthenticationManagerBuilder class as argument will be called first and after that it will call the other configure method with HttpSecurity method.

public void configure(AuthenticationManagerBuilder auth) throws Exception {
//                .passwordEncoder(new BCryptPasswordEncoder())

NOTE: In the above configure method, keep the passwordEncoder line commented, else the user authentication will not work. It was throwing user authentication failed when we enable the BCryptPasswordEncoder class.

After the above configure method the below configure method will be called

protected void configure(HttpSecurity http) throws Exception {

LDAP Data Interchange Format file

The ldif file contains the user repository for the users which are allowed to access the application with respective users details, groups and other permissions.

Refer to the file here: test-server.ldif

Configurations in

If you run the spring guide example as is, you will get an application error on the runtime that the root 389 connection refused error.

2021-11-09 22:52:41.170  INFO 5760 --- [-] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-11-09 22:52:41.182  INFO 5760 --- [-] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms
2021-11-09 22:52:41.865  WARN 5760 --- [-] o.s.b.actuate.ldap.LdapHealthIndicator   : LDAP health check failed

org.springframework.ldap.CommunicationException: localhost:389; nested exception is javax.naming.CommunicationException: localhost:389 [Root exception is Connection refused: connect]
    at ~[spring-ldap-core-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at ~[spring-ldap-core-2.3.3.RELEASE.jar:2.3.3.RELEASE]
    at ~[spring-ldap-core-2.3.3.RELEASE.jar:2.3.3.RELEASE]

In order to resolve these errors we need to add the following configurations:


The port for ldap server is mentioned with the spring.ldap.emebedded.port key and

the ldif file location is in classpath is given with spring.ldap.embedded.ldif key and

the root DN is given by the last key spring.ldap.embedded.base-dn

Once the above configuration is added, then on application startup, the browser will show a login page with /login URL and it will only accept users given as part of the test-server.ldif file.

You can find the complete code base in Github here.

Please do suggest more content topics of your choice and share your feedback. Also subscribe and appreciate the blog if you like it.

3,081 views0 comments

Recent Posts

See All


bottom of page