Spring-LDAP 的例子

Spring-LDAP

下载地址 :http://www.springframework.org/ldap

用Spring LDAP最小需要:

  • spring-ldap(spring-ldap包)
  • spring-core(用于框架内部的丰富的工具类)
  • spring-beans(方便操作java beans的接口和类)
  • spring-context(增加通过一致API为应用对象获取资源的能力)
  • spring-dao(使经常性的错误处理跟使用中的数据访问分开的异常处理机制)
  • commons-logging(简单的日志处理,内部使用)

UserDaoLdapImpl

Java代码
  1. package cn.com.ldap;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.naming.NamingException;  
  6. import javax.naming.directory.Attributes;  
  7.   
  8. import org.springframework.ldap.core.AttributesMapper;  
  9. import org.springframework.ldap.core.LdapTemplate;  
  10.   
  11. import cn.com.ldap.Preson.Person;  
  12.   
  13. /** 
  14.  * @author Wangyaodi version 1.0 2008-6-12 | 下午02:55:25 
  15.  */  
  16. public class UserDaoLdapImpl {  
  17.     private LdapTemplate ldapTemplate;  
  18.   
  19.     public void setLdapTemplate(LdapTemplate ldapTemplate) {  
  20.         this.ldapTemplate = ldapTemplate;  
  21.     }  
  22.   
  23.     public List getAllPersonNames() {  
  24.         return ldapTemplate.search("", "(objectclass=person)",  
  25.                 new AttributesMapper() {  
  26.                     public Object mapFromAttributes(Attributes attrs)  
  27.                             throws NamingException {  
  28.                         return attrs.get("cn").get();  
  29.                     }  
  30.                 });  
  31.     }  
  32.   
  33. }  

preson:

Java代码
  1. package cn.com.ldap;  
  2.   
  3. public class Preson {  
  4.     public class Person {  
  5.   
  6.         private String cn;  
  7.   
  8.         private String sn;  
  9.   
  10.         public String getCn() {  
  11.             return cn;  
  12.         }  
  13.   
  14.         public void setCn(String cn) {  
  15.             this.cn = cn;  
  16.         }  
  17.   
  18.         public String getSn() {  
  19.             return sn;  
  20.         }  
  21.   
  22.         public void setSn(String sn) {  
  23.             this.sn = sn;  
  24.         }  
  25.   
  26.     }  
  27.   
  28. }  

applicationContext.xml

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="contextSource"  
  5.         class="org.springframework.ldap.core.support.LdapContextSource">  
  6.         <property name="url" value="ldap://192.168.16.XXX:389" />  
  7.         <property name="base" value="OU=XXX,OU=XXX,OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX" />  
  8.         <property name="userName" value="XXX@headquarter" />  
  9.         <property name="password" value="XXX" />  
  10.     </bean>  
  11.   
  12.     <bean id="ldapTemplate"  
  13.         class="org.springframework.ldap.core.LdapTemplate">  
  14.         <constructor-arg ref="contextSource" />  
  15.     </bean>  
  16.   
  17.     <bean id="userDao" class="cn.com.ldap.UserDaoLdapImpl">  
  18.         <property name="ldapTemplate">  
  19.             <ref bean="ldapTemplate" />  
  20.         </property>  
  21.     </bean>  
  22. </beans>  

main 

Java代码
  1. public static void main(String[] args) {  
  2.         ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");  
  3.         UserDaoLdapImpl userDao = (UserDaoLdapImpl)cxt.getBean("userDao");  
  4.         List users = userDao.getAllPersonNames();  
  5.         System.out.println(users.size());  
  6.     } 

posted on 2009-04-24 14:31 刘铮 阅读(6172) 评论(0)  编辑  收藏 所属分类: Ldap


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论