title: spring ldap security without xml
body: <p>This documentation explains how to configure Spring-Security LDAP:</p>
  
  <p><a href="http://docs.spring.io/spring-security/site/docs/3.2.4.CI-SNAPSHOT/reference/htmlsingle/#ldap" rel="noreferrer">http://docs.spring.io/spring-security/site/docs/3.2.4.CI-SNAPSHOT/reference/htmlsingle/#ldap</a></p>
  
  <blockquote>
  <pre><code>3.4.5. Spring Bean Configuration
  
  &lt;bean id="contextSource"
          class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"&gt;
    &lt;constructor-arg value="ldap://monkeymachine:389/dc=springframework,dc=org"/&gt;
    &lt;property name="userDn" value="cn=manager,dc=springframework,dc=org"/&gt;
    &lt;property name="password" value="password"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="ldapAuthProvider"
      class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"&gt;
   &lt;constructor-arg&gt;
     &lt;bean class="org.springframework.security.ldap.authentication.BindAuthenticator"&gt;
       &lt;constructor-arg ref="contextSource"/&gt;
       &lt;property name="userDnPatterns"&gt;
         &lt;list&gt;&lt;value&gt;uid={0},ou=people&lt;/value&gt;&lt;/list&gt;
       &lt;/property&gt;
     &lt;/bean&gt;
   &lt;/constructor-arg&gt;
   &lt;constructor-arg&gt;
     &lt;bean
       class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator"&gt;
       &lt;constructor-arg ref="contextSource"/&gt;
       &lt;constructor-arg value="ou=groups"/&gt;
       &lt;property name="groupRoleAttribute" value="ou"/&gt;
     &lt;/bean&gt;
   &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  </code></pre>
  </blockquote>
  
  <p>how can we achieve this without xml?
  Here we have a sample which uses a local ldif file:
  <a href="https://github.com/spring-projects/spring-security/blob/master/samples/ldap-jc/src/main/java/org/springframework/security/samples/config/SecurityConfig.java" rel="noreferrer">https://github.com/spring-projects/spring-security/blob/master/samples/ldap-jc/src/main/java/org/springframework/security/samples/config/SecurityConfig.java</a></p>
  
  <p>I've modified SecurityConfig.java as follows:</p>
  
  <pre><code>    public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
          DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldap_url);
          contextSource.setUrl(ldap_user);
          contextSource.setPassword(ldap_password);
  
          DefaultLdapAuthoritiesPopulator ldapAuthoritiesPopulator = new DefaultLdapAuthoritiesPopulator(contextSource, "ou=groups");
          ldapAuthoritiesPopulator.setGroupRoleAttribute("ou");
  
          LdapAuthenticationProviderConfigurer&lt;AuthenticationManagerBuilder&gt; ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
  
          ldapAuthenticationProviderConfigurer
              .userDnPatterns("uid={0},ou=people")
              .groupSearchBase("ou=groups")
              .contextSource(contextSource)
              .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator);
      }
  }
  </code></pre>
  
  <p>but when I login using the web form, I get this error:</p>
  
  <pre><code>java.lang.NullPointerException
      at java.util.Hashtable.&lt;init&gt;(Hashtable.java:296)
      at org.springframework.ldap.core.support.AbstractContextSource.getAuthenticatedEnv(AbstractContextSource.java:499)
      at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:114)
      at org.springframework.ldap.core.support.AbstractContextSource.getContext(AbstractContextSource.java:110)
      at org.springframework.security.ldap.authentication.BindAuthenticator.bindWithDn(BindAuthenticator.java:112)
  </code></pre>
  
  <p>Is there a similar documentation to <a href="http://docs.spring.io/spring-security/site/docs/3.2.4.CI-SNAPSHOT/reference/htmlsingle/#ldap" rel="noreferrer">http://docs.spring.io/spring-security/site/docs/3.2.4.CI-SNAPSHOT/reference/htmlsingle/#ldap</a> explaining how to achieve this without spring xml?</p>