title: Spring Security: LDAP Authentication
body: <p>I want to use spring security ldap authentication in my web application.On login page i will ask user to enter his/her username and password. Now using this credentials if application is able to connect to the ldap server then user is valid user and i will show him/her home page. but if credentials are wrong then i have to redirect to the login page with error message.</p>
  
  <p>Can anyone tell me how can i achieve this using spring security ldap authentication.</p>
  
  <p>My Spring-security.xml file </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/security"
      xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
             http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"&gt;
  
      &lt;http pattern="/css/**" security="none" /&gt;
      &lt;http pattern="/images/**" security="none" /&gt;
      &lt;http pattern="/index.jsp" security="none" /&gt;
      &lt;http pattern="/js/**" security="none" /&gt;
  
      &lt;http use-expressions="false"&gt;
          &lt;csrf disabled="true" /&gt;
          &lt;intercept-url pattern="/**" access="ROLE_ADMIN" /&gt;
          &lt;form-login login-page='/index.jsp' login-processing-url="/j_spring_security_check" 
              username-parameter="username" 
              password-parameter="password" 
              default-target-url='/login.do'
              authentication-failure-url="/index.jsp?error=1"
              always-use-default-target='true' /&gt;
          &lt;logout invalidate-session="true" logout-url="/j_spring_security_logout" /&gt;
          &lt;http-basic /&gt;
      &lt;/http&gt;
  
  
      &lt;authentication-manager&gt;
          &lt;authentication-provider ref="authProvider"&gt;&lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  
      &lt;beans:bean id="authProvider" class="com.abc.web.security.CustomAuthentication" /&gt;
  
      &lt;global-method-security pre-post-annotations="enabled" /&gt;
  &lt;/beans:beans&gt;
  </code></pre>