title: spring ldap template authentication
body: <p>I am trying to authenticate user via spring ldap. Following is the code to initialise ldap template.</p>
  
  <pre><code>contextSource = new LdapContextSource();
  contextSource.setUrl("ldaps://ldap.example.com");
  contextSource.setBase("DC=example,DC=com");
  contextSource.setUserDn("backend-app");
  contextSource.setPassword("password");
  contextSource.afterPropertiesSet();
  
  PoolingContextSource pooledContextSource = new PoolingContextSource(); 
  pooledContextSource.setDirContextValidator(new 
  DefaultDirContextValidator());
  pooledContextSource.setContextSource(contextSource);
  
  ldapTemplate =  new LdapTemplate(pooledContextSource); 
  ldapTemplate.afterPropertiesSet();
  </code></pre>
  
  <p>When i try to use ldapTemplate authenticate method it returns false.</p>
  
  <pre><code>// below line fails
  ldapTemplate.authenticate("OU=Service Accounts,OU=Pseudo-Users", "frontend-web", "password");
  </code></pre>
  
  <p>But when i use directory context it works</p>
  
  <pre><code>DirContext ctx = null;
  try {
    ctx = contextSource.getContext("frontend-web", "password");
    return true;
  } catch (Exception e) {
    logger.error("Login failed", e);
    return false;
  } finally {
    LdapUtils.closeContext(ctx);  
  }
  </code></pre>
  
  <p><strong>Question 1 :</strong> Is there anyway to make ldaptemplate authenticate method to work?</p>
  
  <p><strong>Question 2 :</strong> Why don't we have to provide baseDn when we use DirectoryContext directly. How does ldap know where to find the user "frontend-web". Does it search the whole directory for the user "frontend-web"</p>
  
  <p>Can anybody help. </p>