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 劉錚 閱讀(6197) 評論(0)  編輯  收藏 所屬分類: Ldap

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 姜堰市| 遵义县| 齐河县| 泰安市| 扎囊县| 尚志市| 郑州市| 宁化县| 会同县| 焦作市| 辽宁省| 五河县| 柳林县| 塔城市| 新兴县| 始兴县| 黔西县| 定襄县| 达州市| 平山县| 峡江县| 梧州市| 女性| 武夷山市| 柯坪县| 平山县| 湖州市| 广州市| 祥云县| 科尔| 高碑店市| 浮梁县| 南阳市| 建水县| 河池市| 公安县| 隆安县| 磴口县| 丰宁| 湘乡市| 福州市|