title: LDAP - AD Username Password configuration for context-source
body: <p>I am using Spring Security to authenticate against Active Directory using LDAP protocol. Following code works well in authentication and setting up LDAP templates too (springSecurity.xml) :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/security"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:beans="http://www.springframework.org/schema/beans" 
              xmlns:ldap="http://www.springframework.org/schema/ldap"
              xsi:schemaLocation="
              http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd 
              http://www.springframework.org/schema/ldap 
              http://www.springframework.org/schema/ldap/spring-ldap.xsd"&gt;
  
      &lt;http use-expressions="true"&gt;
  
          &lt;intercept-url pattern="/login" access="permitAll" /&gt;
          &lt;intercept-url pattern="/authenticated" access="isAuthenticated()" /&gt;
  
          &lt;form-login login-page="/login" default-target-url="/authenticated"
              authentication-failure-url="/login?error=true" /&gt;
          &lt;logout /&gt;
  
  
      &lt;/http&gt;
  
      &lt;beans:bean
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;beans:property name="location"&gt;
              &lt;beans:value&gt;classpath:/ldap.properties&lt;/beans:value&gt;
          &lt;/beans:property&gt;
          &lt;beans:property name="SystemPropertiesMode"&gt;
              &lt;beans:value&gt;2&lt;/beans:value&gt; &lt;!-- OVERRIDE is 2 --&gt;
          &lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="adAuthenticationProvider"
          class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider"&gt;
          &lt;beans:constructor-arg value="${sample.ldap.domain}" /&gt;
          &lt;beans:constructor-arg value="${sample.ldap.url}" /&gt;
          &lt;beans:property name="useAuthenticationRequestCredentials"
              value="true" /&gt;
          &lt;beans:property name="convertSubErrorCodesToExceptions"
              value="true" /&gt;
      &lt;/beans:bean&gt;
  
  
      &lt;authentication-manager&gt;
          &lt;authentication-provider ref="adAuthenticationProvider" /&gt;
      &lt;/authentication-manager&gt;
  
  &lt;!--  Ldap after authentication --&gt;
  
      &lt;context:property-placeholder location="classpath:/ldap.properties"
          system-properties-mode="OVERRIDE" /&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;ldap:context-source id="contextSource" 
                          password="${sample.ldap.password}"
                          url="${sample.ldap.url}" 
                          username="${sample.ldap.userDn}" 
                          base="${sample.ldap.base}"
                          referral="follow" /&gt;
  
      &lt;ldap:ldap-template id="ldapTemplate"
          context-source-ref="contextSource" /&gt;
  
      &lt;ldap:repositories base-package="com.domain" /&gt;
  
      &lt;beans:bean class="com.service.UserService"&gt;
          &lt;beans:property name="directoryType" value="${sample.ldap.directory.type}" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- Required to make sure BaseLdapName is populated in UserService --&gt;
      &lt;beans:bean
          class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" /&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>Authentication works fine while fetching j_username and j_password from login.jsp. To set up the ldap template i am using username and password attribute defined in properties file, but i wish to use same username and password from spring security. Please guide me as how to bind the Username and Password attribute properties in <strong>ldap:context-source id="contextSource"</strong> to spring security credentials.</p>
  
  <p>The code is little messy, any input for improvement is welcomed. </p>