how to retrieve some field from ldap in spring security
i m new to spring. i want to retrieve some field of user details from ldap and display on the jsp page. how can i retrieve this filed from ldap on page load?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="contextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://url:389" />
<property name="base" value="dc" />
<property name="userName" value="uid=admin,ou=system" />
<property name="password" value="secret" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapContact"
class="org.LDAPContactDAO">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
</beans>
It give me following exception
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapContact' defined in class path resource [springldap.xml]: Cannot resolve reference to bean 'ldapTemplate' while setting bean property 'ldapTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapTemplate' defined in class path resource [springldap.xml]: Cannot resolve reference to bean 'contextSource' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contextSource' defined in class path resource [springldap.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userName' of bean class [org.springframework.ldap.core.support.LdapContextSource]: Bean property 'userName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.SpringFrameworkLDAPClient.main(SpringFrameworkLDAPClient.java:20)
I have write down some class file
package org;
public class ContactDTO {
private String displayName;
// lastName = Person.sn
private String firstName;
private String company;
private String department;
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String toString() {
StringBuffer contactDTOStr = new StringBuffer("Person=[");
contactDTOStr.append(" firstName = " + firstName);
contactDTOStr.append(" ]");
return contactDTOStr.toString();
}
}
//interface ContactDAO
package org;
import java.util.List;
public interface ContactDAO {
public List getAllContactNames();
/*public List getContactDetails(String commonName);*/
}
// LDAPContactDAO
package org;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
public class LDAPContactDAO implements ContactDAO{
@Override
public List getAllContactNames() {
// TODO Auto-generated method stub
return null;
}
/*public List getContactDetails(String objectclass){
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectClass",objectclass));
System.out.println("LDAP Query " + andFilter.encode());
return ldapTemplate.search("", andFilter.encode(),new ContactAttributeMapper());
}*/
}
pack开发者_C百科age org;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
public class SpringFrameworkLDAPClient {
public static void main(String[] args) {
//Resource resource = new ClassPathResource("/SpringLDAPClient/src/com/javaworld/sample/springldap.xml");
//System.out.println(resource.toString());
try {
Resource resource = new ClassPathResource("springldap.xml");
BeanFactory factory = new XmlBeanFactory(resource);
System.out.println(factory.toString() + "\n");
ContactDAO ldapContact = (LDAPContactDAO)factory.getBean("ldapContact");
/*List contactList = ldapContact.getContactDetails("30662");*/
//List contactList =ldapContact.getAllContactNames();
//System.out.println(contactList.size());
/*int count = 0;
for( int i = 0 ; i < contactList.size(); i++){
System.out.print("Email: " + ((ContactDTO) contactList.get(i)).getMail() + " ");
System.out.println("SAP: " + ((ContactDTO) contactList.get(i)).getSap());
count++;
}
System.out.println("\n" + count);
*/
} catch (DataAccessException e) {
System.out.println("Error occured " + e.getCause());
}
}
}
But i am not able display this user details on jsp page? please any body know about this reply
The important part of the error message is:
Error creating bean with name 'contextSource'
defined in class path resource [springldap.xml]:
Error setting property values;
nested exception is org.springframework.beans.NotWritablePropertyException:
Invalid property 'userName'
of bean class [org.springframework.ldap.core.support.LdapContextSource]:
Bean property 'userName' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?
I don't know wich version of Spring LDAP You're using, but userName
seems to be deprecated in version 1.2 and completely removed in version 1.3 - try userDn
instead, and see reference section on DirContext Authentication
And on how to get user details from LDAP try the reference: User Authentication using Spring LDAP
Or even better: use Spring Security with LDAP Authentication.
精彩评论