Hello,
I am bit fresher to Spring-Security/LDAP. Here I am try to connect to my company's Active directory server through Spring Security, but I am getting null for ldapTemplate in my TaxUserAuthenticate class which mean I am not able to connect to AD. Please find the below code. I am sure there must be problem in my configuration file however I am not able to figure out the problem. I have writen the below piece of code in /WEB-INF/tax-servlet.xml which used for my Spring MVC web application.
Thanks for your help.
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSp ringSecurityContextSource">
<constructor-arg value="ldap://servername.r1-core.r1.xxx.net:389/DC=r1-core,DC=r1,DC=aig,DC=net"/>
<property name="userDn" value="user@r1-core"/>
<property name="password" value="pwwd11"/>
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate" >
<constructor-arg ref="contextSource" />
</bean>
<bean id="myAuthenticator" class="com.xxx.tax.util.TaxUserAuthenticate">
<property name="ldapTemplate" ref="ldapTemplate"/>
</bean>
I also tried to write the code in below way to connect but hard luck.
<!-- LDPA in Spring framework
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapC ontextSource">
<property name="url" value="ldap://servername.r1-core.r1.xxx.net:389/DC=r1-core,DC=r1,DC=aig,DC=net?sAMAccountName?sub?(objec tClass=*)" />
<property name="base" value="OU=R1,DC=r1-core,DC=r1,DC=xxx,DC=net" />
<property name="userDn" value="user@r1-core" />
<property name="password" value="pwwd11" />
</bean>
-->
/**
*
*/
package com.xxx.tax.util;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import java.io.UnsupportedEncodingException;
import org.springframework.ldap.core.DistinguishedName;
public class TaxUserAuthenticate {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(final LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public TaxUserAuthenticate() {
// TODO Auto-generated constructor stub
}
public boolean login(
String username, String password){
System.out.println("****username*****"+username+"* ********");
System.out.println("****password*****"+password+"* ********");
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("cn", username));
return ldapTemplate.authenticate(DistinguishedName.EMPTY_ PATH, filter.toString(), password);
}
}