title: Spring Security LDAP authentication user must be a member of an AD group
body: <p>I've configured the Spring Boot Security as per:
  <a href="https://spring.io/guides/gs/securing-web/" rel="nofollow noreferrer">https://spring.io/guides/gs/securing-web/</a></p>
  
  <p>I am able to login using my credentials perfectly. However, I need to add a checking that the AD user must also belong to a specific AD group (ie. <strong><em>AD-this-is-a-specific-group</em></strong>). <strong>On login, if the user does not belong to the specific AD group, then it should return a login error.</strong> </p>
  
  <p>I've been searching for hours now and cannot seem to find a clear way to do this in the <code>WebSecurityConfigurerAdapter</code> , am I using the <code>auth.groupSearchFilter</code> correctly?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>@Configuration 
  @EnableWebSecurity    
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  Environment env;
  
  public LdapContextSource contextSource () {
      LdapContextSource contextSource= new LdapContextSource();
  
      contextSource.setUrl(env.getRequiredProperty("ldap.url"));
      contextSource.setBase(env.getRequiredProperty("ldap.baseDn"));
      contextSource.setUserDn(env.getRequiredProperty("ldap.bindDn"));
      contextSource.setPassword(env.getRequiredProperty("ldap.batchPassword"));
      contextSource.afterPropertiesSet();
      return contextSource;
  }
  
  @Override
  protected void configure(AuthenticationManagerBuilder auth)
          throws Exception {
       auth.ldapAuthentication()
          .userSearchFilter("(cn={0})")           
          .groupSearchBase("OU=Account Groups,OU=ITS Security")
          .groupSearchFilter("(cn=AD-this-is-a-specific-group)") 
          .contextSource(contextSource()); 
  }
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests().anyRequest().fullyAuthenticated()
          .and()
          .formLogin();
  }
  </code></pre>