Spring LDAP操作--目录测试环境搭建

Spring LDAP 是一个用于操作 LDAP 的 Java 框架。它是基于 Spring 的 JdbcTemplate 模式。这个框架能够帮助开发人员简化 looking up,closing contexts,looping through NamingEnumerations,encoding/decoding values与 filters 等操作。(理论上市面所有目录产品都适用,什么?你不知道ldap目录,请建议绕行)

 

下面是我搭建的一个简单的java测试目录的环境,只是试下能不能连上ldap,以及能否进行简单操作。功能比较简单

所需jar包:见附件工程

applicationContext.xml配置

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  7.   
  8.     <bean id="contextSource"    
  9.         class="org.springframework.ldap.core.support.LdapContextSource">    
  10.         <property name="url" value="ldap://10.201.4.*:389" />    
  11.         <property name="base" value="o=*" />    
  12.         <property name="userDn" value="cn=*"/>    
  13.         <property name="password" value="***" />    
  14.     </bean>    
  15.     
  16.     <bean id="ldapTemplate"    
  17.         class="org.springframework.ldap.core.LdapTemplate">    
  18.         <constructor-arg ref="contextSource" />    
  19.     </bean>    
  20.       
  21.   
  22.     <bean id="personDao" class="springLdapContext.PersonDaoImpl">  
  23.           <property name="ldapTemplate">    
  24.             <ref bean="ldapTemplate" />    
  25.         </property>    
  26.   
  27.     </bean>  
  28. </beans>  

 另外就是一个简单的操作类了PersonDaoImpl

Java代码   收藏代码
  1. public class PersonDaoImpl implements PersonDao {  
  2.   
  3.     private LdapTemplate ldapTemplate;  
  4.   
  5.     public static void main(String[] args) {  
  6.         ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");     
  7.         PersonDaoImpl personDao = (PersonDaoImpl)cxt.getBean("personDao");     
  8.         List users = personDao.getAllPersonNames();     
  9.         System.out.println(users.size());     
  10.   
  11.     }  
  12.       
  13.       
  14.     /* 
  15.      * @see PersonDao#getAllPersonNames() 
  16.      */  
  17.     public List getAllPersonNames() {  
  18.         EqualsFilter filter = new EqualsFilter("objectclass""person");  
  19.         return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), new AttributesMapper() {  
  20.             public Object mapFromAttributes(Attributes attrs) throws NamingException {  
  21.                 return attrs.get("cn").get();  
  22.             }  
  23.         });  
  24.     }  
  25.   
  26.         public void setLdapTemplate(LdapTemplate ldapTemplate) {  
  27.         this.ldapTemplate = ldapTemplate;  
  28.     }  
  29. }  

 

 如果你查询的路径设置正确且下面有用户,应该就能输出用户数量了。注

<property name="base" value="o=*" />  base即查找的根路径


原文:http://xrb2008.iteye.com/blog/650632

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值