private LdapContextSource getLdapContextSource() throws Exception {
   LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(ldapURL);
   ldapContextSource.setUserDn(ldapBindDN);
   ldapContextSource.setPassword(ldapBindPassword);
   ldapContextSource.setReferral(ldapReferral);
   ldapContextSource.setCacheEnvironmentProperties(false);
   ldapContextSource.setAnonymousReadOnly(false);
   ldapContextSource.setPooled(true);
   ldapContextSource.afterPropertiesSet();
   return ldapContextSource;
 }
 /**
  * Provides a configured LdapTemplate instance that can be used to perform any ldap based
  * operations against the Senate LDAP. This should typically be autowired into DAO layer classes.
  */
 @Bean(name = "ldapTemplate")
 public LdapTemplate ldapTemplate() {
   if (ldapUrl == null || ldapUrl.isEmpty()) {
     throw new BeanInitializationException(
         "Cannot instantiate LDAP Template because ldap.url in the properties file is not set.");
   }
   logger.info("Configuring ldap template with url {}", ldapUrl);
   LdapContextSource ldapContextSource = new LdapContextSource();
   ldapContextSource.setUrl(ldapUrl);
   ldapContextSource.afterPropertiesSet();
   ldapContextSource.setAnonymousReadOnly(true);
   return new LdapTemplate(ldapContextSource);
 }