title: SpringBoot not authenticating with Active Directory
body: <p>I'm new Active Directory Authentication MS 2008 Server via spring boot and can't find a clean example that works.   I have tried several to no avail.  Hopefully someone has done this correctly and can tell me what I'm doing wrong or missing. With login process, no user info coming back nor authentication to the ADS.  Also while here if there is a way to pull the users from the active directory to populate a users table.  Based on the security configuration I can get the login page to come up but it doesn't appear to be authenticating nor pulling the user profile back this based on looking at the data via the debugger.  Using Spring STS IDE to develop in.   Thanks In Advance </p>
  
  <pre><code>**Application.Properties
  ######### LDAP security for company   ###############
  
  spring.ldap.urls=ldap://dc1.domain.com:389/
  spring.ldap.base= dc=domain, dc=com
  spring.ldap.username=srvcacct
  spring.ldap.password=password
  spring.ldap.user.dn.pattern = uid={0}
  spring.ldap.enabled=true
  
  spring.ldap.embedded.base-dn=dc=domain,dc=com
  spring.ldap.embedded.port=389
  server.port=8443
  
  server.ssl.trust-store=tomcat.cer
  server.ssl.trust-store-password=tomcat2018
  server.ssl.trust-store-provider=domain.com
  
  *** WebSecurity ***
  @Override
  public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/static/**");
  }
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
  http
      .authorizeRequests()
      .antMatchers("/login**").permitAll()
      .antMatchers("/profile/**").fullyAuthenticated() 
      .antMatchers("/").permitAll()
      //.antMatchers("/index**").permitAll()
      //.antMatchers("/static**").permitAll()
      //.antMatchers("/**").fullyAuthenticated()
      .and()
      .formLogin()
              .loginPage("/login")
              .failureUrl("/login?error")
              .permitAll()
              .and()
      .logout()
              .invalidateHttpSession(true)
              .deleteCookies("JSESSIONID")
              .permitAll();
  
          http.csrf().disable();
      }
  
      @Autowired
      protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
          authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailsService());
      }
  
      @Bean
      public AuthenticationManager authenticationManager() {
          return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
      }
  
      @Bean
      public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
          ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(ldapBaseDn, ldapURL);
          provider.setConvertSubErrorCodesToExceptions(true);
          provider.setUseAuthenticationRequestCredentials(true);
          provider.setAuthoritiesMapper(new SimpleAuthorityMapper());
          System.out.print("In AuthenticationProvider: " + provider + ldapBaseDn + ldapURL);
          return provider;
      }
  
  }
  
  ** POM file
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
    &lt;groupId&gt;com.domain&lt;/groupId&gt;
    &lt;artifactId&gt;lean&lt;/artifactId&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.6.RELEASE&lt;/version&gt; 
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
    &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
              &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
              &lt;artifactId&gt;mssql-jdbc&lt;/artifactId&gt;
              &lt;version&gt;6.2.2.jre8&lt;/version&gt;&lt;!--$NO-MVN-MAN-VER$--&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!--  This is for the Security Spring Boot --&gt;      
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.ldap&lt;/groupId&gt;
              &lt;artifactId&gt;spring-ldap-core&lt;/artifactId&gt;
          &lt;/dependency&gt;   
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-ldap&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
      &lt;!-- JSON --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.json&lt;/groupId&gt;
              &lt;artifactId&gt;json&lt;/artifactId&gt;
          &lt;/dependency&gt;           
  
          &lt;!-- Embedded H2 database used for testing --&gt;
          &lt;!-- test? --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
              &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;  
          &lt;/dependency&gt;
  
    &lt;/dependencies&gt;
    &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
      &lt;finalName&gt;lean&lt;/finalName&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  
  **Login Page **
  &lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  
  
  &lt;c:set var="contextPath" value="${pageContext.request.contextPath}"/&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --&gt;
      &lt;meta name="description" content=""&gt;
      &lt;meta name="author" content=""&gt;
  
      &lt;title&gt;Log in with your credentials&lt;/title&gt;
  
      &lt;link rel="stylesheet" href="static/css/bootstrap.min.css"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div class="container"&gt;
  
      &lt;form method="POST" action="${contextPath}/login" class="form-signin"&gt;
          &lt;h2 class="form-heading"&gt;Lean Log in&lt;/h2&gt;
  
          &lt;div class="form-group ${error != null ? 'has-error' : ''}"&gt;
              &lt;span&gt;&lt;/span&gt;
              &lt;input name="username" required="" onchange="try{setCustomValidity('')}catch(e){}" oninvalid="setCustomValidity('Please enter a valid userID!')" type="text" class="form-control" placeholder="username"
                     autofocus="true"/&gt;
              &lt;input name="password" required="" onchange="try{setCustomValidity('')}catch(e){}" oninvalid="setCustomValidity('Please enter a password!')" type="password" class="form-control" placeholder="password"/&gt;
              &lt;span&gt;${errorMsg}&lt;/span&gt;
  
              &lt;button class="btn btn-lg btn-primary btn-block" type="submit"&gt;Log In&lt;/button&gt;
          &lt;/div&gt;
  
      &lt;/form&gt;
  
  &lt;/div&gt;
  &lt;!-- /container --&gt;
  &lt;script src="static/js/jquery-3.2.1.slim.min.js"&gt;&lt;/script&gt;
  &lt;script src="static/js/bootstrap.min.js"&gt;&lt;/script&gt;&lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is how I fixed the issue Part of the issue was that I'm using Spring 5.6 version.
  Here is my fix:</p>
  
  <pre><code> @Override
    protected void configure(HttpSecurity http) throws Exception {
  
      http
          .authorizeRequests()
              .antMatchers("/admin/**").hasAnyAuthority("ADMIN")
              .antMatchers("/user/**").hasAnyAuthority("ADMIN", "USER")
              .antMatchers("/profile/**").fullyAuthenticated() 
              .antMatchers("/static/**").permitAll()
              .antMatchers("/**").fullyAuthenticated()
          .and()
            .formLogin()
                  .loginPage("/login")
                  .failureUrl("/login?error")
                  .permitAll()
                  .and()
            .logout()
                  .invalidateHttpSession(true)
                  .deleteCookies("JSESSIONID")
                  .permitAll();
  
          http.csrf().disable();
    }
  
    @Configuration
    protected static class AuthenticationConfiguration extends
            GlobalAuthenticationConfigurerAdapter {
  
        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            ActiveDirectoryLdapAuthenticationProvider provider=
                    new ActiveDirectoryLdapAuthenticationProvider("mydomain.com", "ldap://domaincontroller.mydomain.com:389");
            provider.setConvertSubErrorCodesToExceptions(true);
            provider.setUseAuthenticationRequestCredentials(true);
  
            auth.authenticationProvider(provider);
        }
    }
  
  }
  </code></pre>