SpringLdap访问Ldap服务

    技术2022-05-19  22

    一、使用SpringLdap访问LDAP服务所需jar包

          1、spring-ldap-1.2.1.jar (springLdap的核心包)

    (spring-ldap已经有了1.3.1的版本,但是我建议还是用1.2.1的版本,因为1.3.1的版本的LdapTemplate中的search方法,不支持第一个参数为空,在运行的时候会报org.springframework.ldap.NameNotFoundException的异常)

          2、spring-beans.jar

          3、spring-context.jar

          4、spring-core.jar

          5、spring-dao.jar 

     (使用这个jar包时,要看一下这个jar包中org/springframework/dao目录下有没有EmptyResultSizeDataAccessException类,如果没有这个类的话,运行的时候会报错。)

          6、commons-logging.jar

          7、commons-lang.jar

          8、commons-beanutils.jar

    二、示例代码

      1、Person类的代码:

          package com.ladp.model; /** * 2011-03-04 * @author vicky * */ public class Person { private String cn; private String sn; public String getCn() { return cn; } public void setCn(String cn) { this.cn = cn; } public String getSn() { return sn; } public void setSn(String sn) { this.sn = sn; } }

     

     

      2、UserDaoImpl类的代码:

          package com.ladp.dao; import java.util.List; import javax.naming.NamingException; import javax.naming.directory.Attributes; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; /** * 2011-03-04 * @author vicky */ public class UserDaoImpl { private LdapTemplate ldapTemplate; public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } public List getAllContactNames() { return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper(){ public Object mapFromAttributes(Attributes attrs) throws NamingException{ return attrs.get("cn").get(); } }); } }

     

     

      3、 applicationContext.xml配置文件:

          <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="ladpContextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://localhost:389" /> <property name="base" value="dc=lcl,dc=com"></property> <property name="userDn" value="cn=admin,dc=lcl,dc=com" /> <property name="password" value="secret" /> </bean> <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> <property name="contextSource" ref="ladpContextSource" /> </bean> <bean id="userDao" class="com.ladp.dao.UserDaoImpl"> <property name="ldapTemplate"> <ref bean="ldapTemplate" /> </property> </bean> </beans>

     

       4、测试类代码:

          package com.ladp; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ladp.dao.UserDaoImpl; public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDaoImpl userDao = (UserDaoImpl)cxt.getBean("userDao"); List users = userDao.getAllContactNames(); System.out.println("成功!!!!!!!! 获取用户的个数:"+users.size()); } }

     

     

    注:如果在测试过程中,出现LDAP服务连接异常,请查看LDAP服务器是否开启。

    开启LDAP服务命令:CMD 进入到C:/OpenLDAP 下(LDAP服务安装的目录),运行命令 slapd -d 1       用可以看到控制台下打印一片信息,openldap 默认是用的 Berkeley DB 数据库存储目录数据的。

     

     

     


    最新回复(0)