`
jacally
  • 浏览: 760533 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

LDAP实例 转载

    博客分类:
  • JAVA
阅读更多
java 代码
  1. package test.coral.core.ldap;  
  2.   
  3. //引入LDAP的包  
  4. import java.util.Enumeration;  
  5. import java.util.Hashtable;  
  6.   
  7. import javax.naming.Context;  
  8. import javax.naming.NamingEnumeration;  
  9. import javax.naming.directory.Attribute;  
  10. import javax.naming.directory.Attributes;  
  11. import javax.naming.directory.DirContext;  
  12. import javax.naming.directory.InitialDirContext;  
  13. import javax.naming.directory.SearchControls;  
  14. import javax.naming.directory.SearchResult;  
  15.   
  16. import org.apache.commons.lang.StringUtils;  
  17.   
  18. //import mm.splitString;  
  19.   
  20. public class JNDISearch {  
  21.     public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory"// 驱动  
  22.   
  23.     public static String MY_HOST = "ldap://localhost:389"; // 主机地址和端口  
  24.   
  25.     public static String MY_SEARCHBASE = "dc=xxx,dc=com"// 基点入口  
  26.   
  27.     public static String MY_FILTER = "uid=txhzm"// 过滤条件  
  28.   
  29.     public static String MGR_DN = "cn=admin,dc=gzbd1b,dc=com"// 用户名  
  30.   
  31.     public static String MGR_PW = "pwd"// 密码  
  32.   
  33.     public static String MY_ATTRS[] = { "uid","cn","userpassword","mail"};  
  34.   
  35.     // StringBuffer res = new StringBuffer(); //用来输入名字,IP地址的对象  
  36.     public static String temp = new String();  
  37.   
  38.     public String search() throws Exception {  
  39.         StringBuffer res = new StringBuffer();  
  40.         try {  
  41.             // 建立连接  
  42.             Hashtable env = new Hashtable();  
  43.             env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);  
  44.             env.put(Context.PROVIDER_URL, MY_HOST);  
  45.             env.put(Context.SECURITY_AUTHENTICATION, "simple"); // 使用简单认证来认证用户  
  46.             env.put(Context.SECURITY_PRINCIPAL, MGR_DN);  
  47.             env.put(Context.SECURITY_CREDENTIALS, MGR_PW);  
  48.             DirContext ctx = new InitialDirContext(env);  
  49.   
  50.             // 设置查询范围并开始查询  
  51.             SearchControls constraints = new SearchControls();  
  52.             constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);  
  53.             NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER,  
  54.                     constraints);  
  55.   
  56.             // 打印查询结果  
  57.             while (results != null && results.hasMore()) {  
  58.                 SearchResult sr = (SearchResult) results.next();  
  59.                 String dn = sr.getName();  
  60.                 if(StringUtils.isBlank(dn)) continue;  
  61.                 dn = dn + "," + MY_SEARCHBASE;  
  62.                 System.out  
  63.                         .println("==============================================");  
  64.                 System.out.println("Distinguished Name is: " + dn);  
  65.   
  66.                 // 打印指定的字段//////////////////////////////////////////////////////////////////  
  67.                 Attributes ar = ctx.getAttributes(dn, MY_ATTRS);  
  68.                 if (ar == null) {  
  69.                     // 对应的uid没有多余的属性  
  70.                     System.out.println("Entry " + dn  
  71.                             + " has none of the specified attributes\n");  
  72.                 } else {  
  73.                     // 开始显示对应的字段  
  74.                     for (int i = 0; i < MY_ATTRS.length; i++) {  
  75.                         Attribute attr = ar.get(MY_ATTRS[i]);  
  76.                         if (attr != null) {  
  77.                             System.out.print(MY_ATTRS[i] + " : ");  
  78.                             for (Enumeration vals = attr.getAll(); vals  
  79.                                     .hasMoreElements();) {  
  80.                                 Object obj = vals.nextElement();  
  81.                                   
  82.                                 System.out.println("\t" + obj);  
  83.                                 res.append(temp + "/");  
  84.                             }  
  85.                         }  
  86.                         System.out.println("\n");  
  87.                     }  
  88.                     // /////////////////////////////////////////////////////////////////////////////////  
  89.   
  90.                     /* 
  91.                      * 打印全部的字段/////////////////////////////////////////////////////////////////// 
  92.                      * Attributes attrs = sr.getAttributes(); 
  93.                      * for(NamingEnumeration ne = attrs.getAll(); 
  94.                      * ne.hasMoreElements(); ){ Attribute attr = (Attribute) 
  95.                      * ne.next(); String attrID = attr.getID(); 
  96.                      * System.out.println(attrID+": "); for(Enumeration vals = 
  97.                      * attr.getAll();vals.hasMoreElements(); ){ 
  98.                      * System.out.println("\t"+vals.nextElement()); } 
  99.                      */// ///////////////////////////////////////////////////////////////////////////////  
  100.                 }  
  101.             }  
  102.         } catch (Exception e) {  
  103.             e.printStackTrace();  
  104.             System.exit(1);  
  105.         }  
  106.         System.out.println(res.toString() + "\n\n\n\n ---- end ----");  
  107.   
  108.         // splitString sp = new splitString();  
  109.         // System.out.println("一共有"+sp.splitString(res.toString()).length+"个返回");  
  110.         // //打印显示结果,计算返回的数组值  
  111.         //return sp.splitString(res.toString());  
  112.         return res.toString();  
  113.   
  114.     }  
  115.   
  116.     ///////////////////////////////////////////////////////////////////////////////////////////  
  117.     // 使用正则表达式来分拣提取的字符串 ///////////////////////////////////  
  118.     ///////////////////////////////////////////////////////////////////////////////////////////  
  119.   
  120.       
  121.       
  122.     public static void main(String args[]) {  
  123.         JNDISearch search = new JNDISearch();  
  124.         //System.out.println(myMail.sendMail("libem@163.com", "this is test",   "my \n test"));  
  125.         try {  
  126.             System.out.println(search.search());  
  127.         } catch (Exception e) {  
  128.             // TODO Auto-generated catch block  
  129.             e.printStackTrace();  
  130.         }  
  131.     }     
  132. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics