2011-06-03 48 views
2

我是新來的春天。我想從ldap中檢索一些字段的用戶詳細信息並顯示在jsp頁面上。如何在頁面加載時從ldap中檢索此字段?如何在彈簧安全中從ldap中檢索一些字段

<?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> 

它給我下面的異常

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) 

我已經寫了一些類文件

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(); 
    } 
} 

//接口的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()); 

    }*/ 
} 



package 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()); 
     } 
    } 
} 

但我無法在jsp頁面上顯示此用戶詳細信息?請任何機構知道這個答覆

回答

1

錯誤信息的最重要的部分是:

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? 

我不知道春天的LDAP至極版本您使用,但userName似乎版本被棄用1.2和1.3版完全刪除 - 嘗試userDn代替,並看到DirContext Authentication


參考區段和如何從LDAP獲取用戶的詳細信息請嘗試參考:User Authentication using Spring LDAP

甚至更​​好:使用Spring SecurityLDAP Authentication

+0

感謝您的這些信息。我已經將spring-ldap 1.1更改爲1.3版。基本上我想要從ldap和顯示在jsp頁面上檢索用戶的詳細信息。你有一些例子,以便我更好地理解它嗎? – Raje 2011-06-03 09:29:34

+0

任何機構都有任何想法如何從ldap使用ldap模板檢索用戶詳細信息? – Raje 2011-06-03 12:44:03

+0

@如果還不算太晚,請查看我的更新回答。 – Roadrunner 2011-06-08 10:12:32