title: Use sAMAccountName instead of userPrincipalName in LDAP auth in Spring Boot
body: <p>So I am using the code below to connect to our LDAP server in my spring boot app, I can authorized successfully using the userPrincipalName -&gt; (<strong>LastName.FirstName@enterprise.com</strong>), but I want to use the assigned sAMAccountName instead (<strong>ID00001</strong>).</p>
  <p>I messed around with the setSearchFilter by doing: provider.setSearchFilter(&quot;(sAMAccountName ={0})&quot;)</p>
  <p>but it's not working. (I'm getting bad credentials)</p>
  <pre><code>@EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .antMatcher(&quot;/myapplication&quot;)
              .authorizeRequests()
              .anyRequest().fullyAuthenticated()
              .and()
              .httpBasic();
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
      }
  
      @Bean
      public AuthenticationManager authenticationManager() {
          return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
      }
      @Bean
      public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
          ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(&quot;&quot;, &quot;ldap://test.enterprise.com&quot;,&quot;dc=ORG1, dc=ORG2&quot;);
          provider.setConvertSubErrorCodesToExceptions(true);
          provider.setUseAuthenticationRequestCredentials(true);
          provider.setSearchFilter(&quot;(sAMAccountName={0})&quot;); // THIS DOES NOT WORK
          provider.setSearchFilter(&quot;(userPrincipalName={0})&quot;); // THIS WORKS
          return provider;
      }
  }
  </code></pre>
  <p>EDIT: OK turns out I have been using the wrong field, there is another field: sAMAccountName that has the same value that I should be using, updated the title and question contents.</p>
  <p>EDIT:</p>
  <p>Ok I tried:</p>
  <pre><code>provider.setSearchFilter(&quot;(&amp;(objectClass=user)(cn={0}))&quot;);
  provider.setSearchFilter(&quot;(&amp;(objectClass=user)(sAMAccountName={0}))&quot;);
  provider.setSearchFilter(&quot;(&amp;(objectClass=user)(cn={1}))&quot;);
  provider.setSearchFilter(&quot;(&amp;(objectClass=user)(sAMAccountName={1}))&quot;);
  </code></pre>
  <p>and it's still the same error,</p>
  <p>bad credentials, AcceptSecurityContextError, 52e v2580</p>