Securing Spring Boot applications with LDAP

LDAP is commonly used in Spring Boot applications as a source of authentication and authorization information. In this tutorial we will learn how to secure a simple Spring Boot Web application using an embedded LDAP Server.

Project set up

First of all, in order to build our project we need the following dependencies to run a Web application with LDAP:

<?xml version="1.0" encoding="UTF-8"?><project>

Then the simple REST Controller we will secure is the following one:

public class CustomerController {
  @Autowired CustomerRepository repository;

      method = RequestMethod.GET,
      produces = {"application/json"})
  public List<Customer> findAll() {
    return repository.getData();

  @PostMapping(path = "/", consumes = "application/json", produces = "application/json")
  public ResponseEntity<Customer> addCustomer(@RequestBody Customer customer) throws Exception {;
    return new ResponseEntity<Customer>(customer, HttpStatus.CREATED);

For the sake of brevity, we don’t include the Spring Boot Main class and the CustomerRepository class. Chec out the full source code for this example at the end of the article.

In order to authorize our REST Controller with LDAP, we will need a @Configuration class which extends WebSecurityConfigurerAdapter:

public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
  private String ldapUrls;

  private String ldapBaseDn;

  private String ldapSecurityPrincipal;

  private String ldapPrincipalPassword;

  private String ldapUserDnPattern;

  protected void configure(HttpSecurity http) throws Exception {

  public void configure(AuthenticationManagerBuilder auth) throws Exception {
        .url(ldapUrls + ldapBaseDn)

In the basic example above, any valid username and password belonging to “scientists” group will be authorized to access any HTTP Resource.

If you don’t have an out of the box LDAP configuration, you can just use the free online LDAP server which is available at

The following is what you need to connect to the remote LDAP server:

ldap.enabled = true
ldap.urls= ldap:// 
ldap.base.dn= dc=example,dc=com 
ldap.username= cn=read-only-admin,dc=example,dc=com 
ldap.password= password 
ldap.user.dn.pattern = uid={0}

You can now run the application with:

$ mvn clean install spring-boot:run

If you request the root HTTP GET (http://localhost:8080), a Login form will appear:

Spring boot LDAP tutorial

You can enter any of the available users, such as einstein/password. A simple JSON will be returned:


Using a Local LDAP Server

If you want to use a local LDAP server, all you have to do is changing the with the appropriate settings. For example we can test it against this example LDAP server:

Here is the required to connect to the above Apache DS server:

ldap.user.dn.pattern =uid={0},ou=People

Source code for this article: