title: LDAP authentication in Spring without XML
body: <p>I'm trying to implement a login system in my application, using company's Active Directory, structure of which is similar to the one described <a href="https://stackoverflow.com/questions/37782168/setting-up-ldap-authentication-in-spring-security">here</a> but even more branched out.
  I set it up like this</p>
  
  <pre><code>import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  
  @Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
                  .anyRequest().fullyAuthenticated()
                  .and()
                  .formLogin();
      }
  
      @Override
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
                  .ldapAuthentication()
                      .userDnPatterns("uid={0}")
                      .contextSource()
                          .url("ldap://10.23.1.1:389/dc=dev,dc=company,dc=corp")
                          .and()
                      .passwordCompare()
                          .passwordEncoder(new LdapShaPasswordEncoder())
                          .passwordAttribute("userPassword");
      }
  }
  </code></pre>
  
  <p>When I run the application and try to log in, I get:</p>
  
  <pre><code>Your login attempt was not successful, try again.
  
  Reason: Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C09079A, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v23f0]; remaining name 'uid=my.name@company.hr'
  </code></pre>
  
  <p>I tried to do some research on binding, but all examples I find either use XMLs, or demonstrate a connection to the embedded ldif, neither of which is applicable here.</p>