【问题标题】:how to retrieve some field from ldap in spring security如何在spring security中从ldap中检索一些字段
【发布时间】:2011-06-08 18:11:25
【问题描述】:

我是春天的新手。我想从 ldap 检索一些用户详细信息字段并显示在 jsp 页面上。如何在页面加载时从 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 页面上显示此用户详细信息?请任何人知道这个回复

【问题讨论】:

    标签: java spring jsp ldap


    【解决方案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?
    

    我不知道您使用的是哪个版本的 Spring LDAP,但 userName 似乎在 1.2 版中已被弃用,并在 1.3 版中完全删除 - 请尝试使用 userDn,并参阅 DirContext Authentication 上的参考部分


    关于如何从 LDAP 获取用户详细信息,请尝试参考:User Authentication using Spring LDAP

    甚至更好:使用Spring SecurityLDAP Authentication

    【讨论】:

    • 感谢您提供此信息。我已将 spring-ldap 1.1 更改为 1.3 版本。基本上我想从 ldap 检索用户详细信息并在 jsp 页面上显示。你有一些例子让我更好地理解它吗?
    • 任何人都知道如何使用 ldap 模板从 ldap 检索用户详细信息?
    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2016-03-23
    • 2013-07-17
    • 2017-07-21
    • 2018-10-08
    • 2021-06-03
    相关资源
    最近更新 更多