<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 23,comments - 12,trackbacks - 0
    1.連接  
     
    import  java.util.Hashtable;  
     
    import  javax.naming.Context;  
    import  javax.naming.NamingException;  
    import  javax.naming.directory.DirContext;  
    import  javax.naming.directory.InitialDirContext;  
     
    public  class  ActiveDirectoryConntct1  
    {  
           public  static  void  main(String[]  args)  
           {  
                   String  account="user1";  
                   String  password="password1";  
                   String  root="dc=test,dc=com";  //root  
     
                   Hashtable  env  =  new  Hashtable();  
                   env.put(Context.INITIAL_CONTEXT_FACTORY,  "com.sun.jndi.ldap.LdapCtxFactory");  
                   env.put(Context.PROVIDER_URL,  "ldap://192.168.0.1:389/"  +  root);  
                   env.put(Context.SECURITY_AUTHENTICATION,  "simple");  
                   env.put(Context.SECURITY_PRINCIPAL,  "uid="  +  account  +  ","  +  root);  
                   env.put(Context.SECURITY_CREDENTIALS,  password);  
     
                   DirContext  ctx  =  null;  
                   try  
                   {  
                           ctx  =  new  InitialDirContext(env);  
                           System.out.println("認(rèn)證成功");  
                   }  
                   catch(javax.naming.AuthenticationException  e)  
                   {  
                           System.out.println("認(rèn)證失敗");  
                   }  
                   catch(Exception  e)  
                   {  
                           System.out.println("認(rèn)證出錯:");  
                           e.printStackTrace();  
                   }  
     
                   if(ctx  !=  null)  
                   {  
                           try  
                           {  
                                   ctx.close();  
                           }  
                           catch  (NamingException  e)  
                           {  
                                   //ignore  
                           }  
                   }  
                   System.exit(0);  
           }  
    }  
     
    2.查詢  
    DirContext  ctx  =  ...  
    SearchControls  constraints  =  new  SearchControls();  
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);  
    //constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);  
    NamingEnumeration  en  =  ctx.search("",  "uid=*",  constraints);  //查詢所有用戶  
    while  (en  !=  null  &&  en.hasMoreElements())  
    {  
           Object  obj  =  en.nextElement();  
           if(obj  instanceof  SearchResult)  
           {  
                   SearchResult  si  =  (SearchResult)  obj;  
             
                   System.out.println("name:  "  +  si.getName());  
             
                   Attributes  attrs  =  si.getAttributes();  
                   if  (attrs  ==  null)  
                   {  
                           System.out.println("No  attributes");  
                   }  
                   else  
                   {  
                           for  (NamingEnumeration  ae  =  attrs.getAll();  ae.hasMoreElements();)  
                           {  
                                   Attribute  attr  =  (Attribute)  ae.next();  
                                   String  attrId  =  attr.getID();  
             
                                   for  (Enumeration  vals  =  attr.getAll();vals.hasMoreElements();)  
                                   {  
                                           System.out.print(attrId  +  ":  ");  
                                           Object  o  =  vals.nextElement();  
                                           if(o  instanceof  byte[])  
                                                   System.out.println(new  String((byte[])o));  
                                           else  
                                                   System.out.println(o);  
                                   }  
                           }  
                   }  
           }  
           else  
           {  
                   System.out.println(obj);  
           }  
           System.out.println();  
    }  
    ...  
    ctx.close();  
     
    3.添加  
    DirContext  ctx  =  ...;  
    String  newUserName  =  "user2";  
     
    BasicAttributes  attrs  =  new  BasicAttributes();  
     
    BasicAttribute  objclassSet  =  new  BasicAttribute("objectclass");  
    objclassSet.add("person");    
    objclassSet.add("top");    
    objclassSet.add("organizationalPerson");    
    objclassSet.add("inetOrgPerson");  
     
    attrs.put(objclassSet);  
    attrs.put("sn",  newUserName);  
    attrs.put("uid",  newUserName);  
     
    ctx.createSubcontext("uid="  +  newUserName,  attributes);  
    ...  
     
     
    4.刪除  
    String  account  =  "user2";  
    DirContext  ctx  =  ...;  
    ctx.destroySubcontext("uid="  +  account);  
     
     
    5.修改  
    String  account  =  "user2";  
    String  newDisplayName  =  "newDisplayName";  
     
    ModificationItem  modificationItem[]  =  new  ModificationItem[1];  
    modificationItem[0]  =  
               new  ModificationItem(  
                           DirContext.REPLACE_ATTRIBUTE,  
                           new  BasicAttribute("displayName",  newDisplayName);  
     
    DirContext  ctx  =  ...;  
    ctx.modifyAttributes("uid="  +  account,  modificationItem);  
    posted on 2005-10-27 16:40 my java 閱讀(1380) 評論(1)  編輯  收藏 所屬分類: JNDI

    FeedBack:
    # re: 用jdni 對LDAP讀寫操作代碼
    2007-11-18 11:20 | 雪浪
    謝謝!!!  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲精品人成无码中文毛片| 日韩免费视频观看| 免费夜色污私人影院网站| 久久国产精品免费一区| 无码专区一va亚洲v专区在线| 亚洲av无码成人影院一区| 日本19禁啪啪无遮挡免费动图| 亚洲一级特黄特黄的大片| 最近2019中文字幕mv免费看| 亚洲av无码兔费综合| 免费国产在线观看不卡| 日韩一级片免费观看| 中文字幕亚洲无线码a| 亚洲精品动漫免费二区| 免费无码又爽又刺激高潮| 美女免费视频一区二区三区| 亚洲男人的天堂在线va拉文| 久久青草免费91观看| 亚洲第一页在线观看| 中国一级特黄的片子免费 | 久久精品国产精品亚洲艾| 亚洲已满18点击进入在线观看| 成人免费午夜在线观看| 国产精品亚洲综合| 日韩免费一区二区三区在线播放| 久久综合亚洲色HEZYO国产| 青青操免费在线视频| 亚洲精品免费在线| 免费国产在线观看| 蜜桃视频在线观看免费视频网站WWW | 国产人在线成免费视频| 亚洲国产精品无码第一区二区三区| 亚洲免费视频一区二区三区| 欧洲精品99毛片免费高清观看| 亚洲一区二区三区写真| 亚洲精品国精品久久99热| 1000部夫妻午夜免费| 在线观看亚洲专区| 亚洲视频免费观看| 亚洲一级毛片免费看| 亚洲综合视频在线|