title: How to get LDAP Username in Springboot
body: <p>How to get username when User is trying to Authenticate by LDAP with Active Directory in Springboot.</p>
  
  <p>As i have tried by using below code but getting error as shown below:</p>
  
  <h1>LDAP Config:</h1>
  
  <pre><code>#ldap.url=ldap://localhost:389
  
  #ldap.base.dn=dc=springframework,dc=org
  #ldap.user.dn.pattern=(&amp;(objectClass=user)(userPrincipalName={0})(memberof=CN=GroupACCESS,OU=people, DC=springframework,DC=org))
  </code></pre>
  
  <h1>WebSecurityConfig.java</h1>
  
  <pre><code>    @Configuration
      @EnableWebSecurity
      public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
          private final static Logger log = LogManager.getLogger(WebSecurityConfig.class);
  
          @Value("${ldap.urls}")
          private String ldapUrl;
  
          @Value("${ldap.base.dn}")
          private String ldapDomain;
  
          @Value("${ldap.user.dn.pattern}")
          private String ldapUserDnPattern;
  
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
  
            http.addFilterBefore(
                    new CustomFilter(), UsernamePasswordAuthenticationFilter.class);
  
              http.authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic();
          }
  
          @Override
          protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  
              ActiveDirectoryLdapAuthenticationProvider adProvider = new ActiveDirectoryLdapAuthenticationProvider(
                      this.ldapDomain, this.ldapUrl);
  
              adProvider.setConvertSubErrorCodesToExceptions(true);
              adProvider.setUseAuthenticationRequestCredentials(true);
  
  
              // Checks with the Distinguished Name pattern provided
              if (this.ldapUserDnPattern != null &amp;&amp; this.ldapUserDnPattern.trim().length() &gt; 0) {
  
                  adProvider.setSearchFilter(this.ldapUserDnPattern);
  
                  Authentication auth1 = SecurityContextHolder.getContext().getAuthentication();
                  String userName = auth1.getName();
                  String password = (String)auth1.getCredentials();
  
                  log.info("userName:"+userName);
              }
  
  
              auth.authenticationProvider(adProvider);
  
          }
      }
  </code></pre>
  
  <p>Please find Error log as shown below:</p>
  
  <pre><code> [ERROR] 2018-08-23 15:13:53.376 [restartedMain] SpringApplication - Application run failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
  </code></pre>
  
  <p>Can anyone please look into this and help me on this.</p>