title: Spring boot Active Directory/LDAP connection
body: <p>I already connect with AD from spring boot for login purposes but i can not mak searches. My configuration is</p>
  
  <pre><code>@Configuration
  public class LdapTemplateConfig {
  
      @Bean
      public LdapTemplate ldapTemplate() {
          LdapTemplate ldapTemplate = new LdapTemplate(ldapContextSource());
          return ldapTemplate;
      }
  
      @Bean
      public LdapContextSource ldapContextSource() {
  
          String url = "ldap://127.0.0.1:389";
          String base = "DC=demo1,DC=demo2,DC=demo3,DC=demo4";
  
          LdapContextSource ldapContextSource = new LdapContextSource();
          ldapContextSource.setUrl(url);
          ldapContextSource.setUserDn(
                  "CN=User Name,OU=Common Users OU,OU=RDP enabled Users OU,OU=Operator Users OU,OU=Admin Users OU,DC=demo1,DC=demo2,DC=demo3,DC=demo4");
          ldapContextSource.setPassword("password");
      //  ldapContextSource.setReferral("follow");
          ldapContextSource.afterPropertiesSet();
          return ldapContextSource;
      }
  }
  
  
  @Service
  public class LDAPServiceImpl {
  
      @Autowired
      private LdapTemplate ldapTemplate;
  
      public void getUserDetails(String userName) {
          if (null != ldapTemplate) {
              List&lt;String&gt; vals = ldapTemplate.search(query().where("objectclass").is("person"),
                      new AttributesMapper&lt;String&gt;() {
                          @Override
                          public String mapFromAttributes(Attributes attributes) throws NamingException {
                              return attributes.get("sAMAccountName").get().toString();
                          }
                      });
              for (String s : vals) {
                  log.info("attr : " + s);
              }
          } else {
              log.info("Templates is null");
          }
      }
  }
  </code></pre>
  
  <p>So, when i call the function getUserDetails() from controller it returns "Templates is null"</p>