title: How to authenticate to LDAP using Spring LDAP
body: <p>I'm kind of newbie on Spring Boot and have a Jndi code that authenticates to an LDAP server just fine.  But now i want to migrate my code to Spring LDAP, but get [LDAP: error code 49 - 80090308: LdapErr: DSID-0C09044E, comment: AcceptSecurityContext error, data 52e, v2580] every time.</p>
  <p>So my JNDI code looks something like this:</p>
  <pre><code>    public void connect(String userName, String pwd) throws NamingException, IllegalStateException {
      Hashtable&lt;String, String&gt; env = new Hashtable&lt;String, String&gt;();
      try {
          env.put(LdapContext.CONTROL_FACTORIES, &quot;com.sun.jndi.ldap.ControlFactory&quot;);
          env.put(Context.SECURITY_PRINCIPAL, userName+&quot;@domain.net);
          env.put(Context.SECURITY_CREDENTIALS, pwd);
          env.put(Context.INITIAL_CONTEXT_FACTORY, &quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
          env.put(Context.PROVIDER_URL,  &quot;ldap://server:389&quot;);
          env.put(Context.SECURITY_AUTHENTICATION, &quot;simple&quot;); 
          context = new InitialDirContext(env); 
      } finally {
          if (context !=null) {
              log.info(&quot;Authentication successfull&quot;);
              try {
                  context.close();
                  context = null;
              } catch(NamingException ne) {
                  log.error(ne.getMessage());
                  ne.printStackTrace();
              }
          } else {
              throw new  IllegalStateException(&quot;Can't obtain LDAP context&quot;);
          }
      }
  }
  </code></pre>
  <p>Very straightforward.</p>
  <p>So far i have configured in my Spring boot application.properties file, the following:</p>
  <pre><code>spring.ldap.urls=Ldap://server:389
  spring.ldap.embedded.credential.username=cn=ceadministrator
  spring.ldap.embedded.credential.password=******
  spring.ldap.embedded.base-dn=dc=domain,dc=net
  </code></pre>
  <p>Letting Spring Ldap manage the connection and initialization</p>
  <p>Implemented this in order to search for some user:</p>
  <pre><code>    public List&lt;User&gt; getUser(String userName) throws NamingException, LDAPException {
      LdapQuery query = LdapQueryBuilder.query()
                          .searchScope(SearchScope.SUBTREE)
                          .timeLimit(3000)
                          .countLimit(10)
                          .attributes(&quot;cn&quot;)
                          .base(ldapConfig.getBase())
                          .where(&quot;objectClass&quot;).is(&quot;user&quot;)
                          .and(&quot;sAMAccountName&quot;).is(userName);
                          
      log.info(&quot;ldapTemplate: &quot;+ldapTemplate);
      return ldapTemplate.search(query, new UserAttributesMapper());
  }
  
  private class UserAttributesMapper implements AttributesMapper&lt;User&gt; {
      @Override
      public User mapFromAttributes(Attributes attributes) throws NamingException {
          User user = new User();
          if (attributes == null) {
              log.warn(&quot;atttrs null&quot;);
              return user;
          }
          user.setFirstName((String) attributes.get(&quot;cn&quot;).get());
          Attribute sn = attributes.get(&quot;sAMAccountName&quot;);
          if (sn != null) {
              user.setUserName((String) sn.get());
          }
          return user;
      }
  }
  </code></pre>
  <p>But it throws an AutheticationException:</p>
  <pre><code>javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C09044E, comment: AcceptSecurityContext error, data 52e, v2580
  </code></pre>
  <p>What am i missing/doing wrong?... Please help</p>