爪哇哇

一个软件分析师的博客

JAVA的对LDAP操作代码(以DOMINO为例子)

LdapConnector  连接类

import J2EE技术的爬虫.util.Hashtable;

import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;

public class LdapConnector {
 public final static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
 public final static String HOST_URL = "ldap://128.8.1.8:389";
    public static LdapConnector connector=null;
 
 private DirContext ctx;

 private Hashtable<String,String> env;
   
 public LdapConnector(String principal, String credentials) {
  env = new Hashtable();
  try {
   env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
   env.put(Context.PROVIDER_URL, HOST_URL);
   env.put(Context.SECURITY_AUTHENTICATION, "simple");
   env.put(Context.SECURITY_PRINCIPAL, principal);
   env.put(Context.SECURITY_CREDENTIALS, credentials);
   ctx = new InitialDirContext(env);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  
 }
 
 public  DirContext getDirContext(){
  return ctx;
 }
 
 public static SearchControls getSearchControls(){
  SearchControls constraints;
  constraints = new SearchControls();
  constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
  return constraints;
 }
 
 
 public static LdapConnector  init(){
     synchronized (LdapConnector.class) {
      if(connector==null)
    connector=new LdapConnector("lilb","1"); 
 }  
   return connector;
 }
}

测试类:

import J2EE技术的爬虫.util.ArrayList;

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;

public class LdapText {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub
  String cn="";
  String dn="";
  
  String filter="";
  String searchBase="";
  NamingEnumeration<SearchResult> results;
  
  

DirContext ctx =LdapConnector.init().getDirContext();
        //先拿CN值 ================
        searchBase="";
        filter ="(uid=lilb)";
        results =
   ctx.search(searchBase, filter, LdapConnector.getSearchControls());
  while (results != null && results.hasMore()) {
   SearchResult sr2 =  results.next();
   cn=(String)sr2.getAttributes().get("cn").get();
   dn=sr2.getName();
   System.out.println(dn);
  }
        //=======================
       
        J2EE技术的爬虫.util.List<String> uidList=new ArrayList<String>();
  
        filter =
   "(&(objectClass=dominoGroup)(cn=IT事业部))";
       
   results =
   ctx.search(searchBase, filter, LdapConnector.getSearchControls());
  
  while (results != null && results.hasMore()) {
   SearchResult sr =  results.next();
   System.out.println(sr.getName());
   
   Attributes attrs = sr.getAttributes();
   NamingEnumeration attrsEnum=attrs.get("member").getAll();
   System.out.println("组织里的人员有 :"+attrs.get("member"));
   while (attrsEnum != null && attrsEnum.hasMore()) {
    String str=(String)attrsEnum.next();
    System.out.println(str.equalsIgnoreCase(dn));
    NamingEnumeration<SearchResult> resultsPerson =
     ctx.search(str, "(objectClass=*)", LdapConnector.getSearchControls());//根据DN,再重新查找
    while (resultsPerson != null && resultsPerson.hasMore()) { 
     SearchResult srPerson =  resultsPerson.next();
     System.out.println(srPerson.getAttributes().get("cn").get());//里面人的CN
     System.out.println(srPerson.getAttributes().get("uid").get()+"@");//里面人的UID
     uidList.add(srPerson.getAttributes().get("uid").get().toString().toLowerCase());
    }
   }
   System.out.println("本组织是否存在这用户 :"+uidList.contains("lilb"));
   
   
   
  }
 }

}

posted on 2007-04-10 14:21 李立波 阅读(1639) 评论(0)  编辑  收藏


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


网站导航:
 

My Links

Blog Stats

News

常用链接

留言簿(5)

随笔分类

随笔档案

文章档案

相册

搜索

最新评论

阅读排行榜

评论排行榜