title: Spring boot and LDAP with SHA512 password
body: <p>I'm writing a little application for use it as authentication server as microservice. (We are dividing an old monolithic app). </p>
  
  <p>This app has to login through a LDAP server (which is protected by an username and password). And ever user password is stored as SHA512 hash. But I always get an "BAD CREDENTIALS" error</p>
  
  <p>Here is my code</p>
  
  <blockquote>
    <p>WebSecurityConfig.java</p>
  </blockquote>
  
  <pre><code>@Configuration
      public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
  
              http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin();
  
          }
  
          @Override
          protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
              auth.authenticationProvider(ldapAuthProvider());
              auth.ldapAuthentication().passwordCompare().passwordEncoder(new LdapShaPasswordEncoder()).passwordAttribute("userPassword");
              super.configure(auth);
          }
  
  
      public LdapAuthenticationProvider ldapAuthProvider() {
  
          DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://myldaphost:389/");
          contextSource.setUserDn("cn=myuser");
          contextSource.setPassword("mypassword");
  
  
  
          FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch("", "(&amp;amp;(cn={0})(estado=activo))", contextSource);
  
  
          BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource);
          bindAuthenticator.setUserSearch(userSearch);
  
  
          LdapAuthenticationProvider provider = new LdapAuthenticationProvider(bindAuthenticator);
  
          return provider;
  
      }
  }
  </code></pre>
  
  <blockquote>
    <p>pom.xml</p>
  </blockquote>
  
  <pre><code>&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/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;gs-authenticating-ldap&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&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.7.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;!-- tag::security[] --&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;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;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.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
      &lt;!-- end::security[] --&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;/build&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  
  <blockquote>
    <p>HomeController.java</p>
  </blockquote>
  
  <pre><code>@RestController
  public class HomeController {
  
      @RequestMapping("/")
      public String index() {
          return "Welcome to the home page!";
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>applicationContext-security.xml from the OLD APP which currently works</p>
  </blockquote>
  
  <pre><code>&lt;beans&gt;
  
  
  &lt;!-- SECURITY ENABLED --&gt;
  &lt;beans profile="security-enabled"&gt;
      &lt;!-- Data  Data Base &amp; LDAP--&gt;      
      &lt;bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"&gt;
          &lt;constructor-arg&gt;
              &lt;bean id="bindAuthenticator" class="org.springframework.security.ldap.authentication.BindAuthenticator"&gt;
                  &lt;constructor-arg ref="contextSource" /&gt;
                  &lt;property name="userSearch" ref="userSearch" /&gt;
              &lt;/bean&gt;
          &lt;/constructor-arg&gt;
      &lt;/bean&gt;
  
      &lt;bean id="userSearch"
          class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"&gt;
          &lt;constructor-arg index="0" value="" /&gt;
          &lt;constructor-arg index="1" value="(&amp;amp;(cn={0})(estado=activo))" /&gt;
          &lt;constructor-arg index="2" ref="contextSource" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="contextSource"
          class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"&gt;
          &lt;constructor-arg value="myurl" /&gt;
          &lt;property name="userDn" value="myuser" /&gt;
          &lt;property name="password" value="mypassword" /&gt;
      &lt;/bean&gt;
  
      &lt;security:authentication-manager alias="authenticationManager"&gt;
          &lt;security:authentication-provider ref="ldapAuthProvider" /&gt;
      &lt;/security:authentication-manager&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p></p>