<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("認證成功");  
                   }  
                   catch(javax.naming.AuthenticationException  e)  
                   {  
                           System.out.println("認證失敗");  
                   }  
                   catch(Exception  e)  
                   {  
                           System.out.println("認證出錯:");  
                           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 | 雪浪
    謝謝!!!  回復  更多評論
      
    主站蜘蛛池模板: 无限动漫网在线观看免费| 亚洲精品乱码久久久久66| 午夜一区二区免费视频| 亚洲一区二区三区在线播放| 午夜亚洲www湿好大| 亚洲av永久无码一区二区三区| 亚洲youwu永久无码精品 | 成全在线观看免费观看大全 | 9久热精品免费观看视频| 免费人成网站在线观看10分钟| 亚洲国产高清在线一区二区三区| 亚洲导航深夜福利| 中国好声音第二季免费播放| 啦啦啦手机完整免费高清观看| 亚洲av无码一区二区三区乱子伦| 亚洲国产美女精品久久久 | 亚洲人成无码网WWW| 久久国产精品免费| 好看的亚洲黄色经典| 9277手机在线视频观看免费| 国产亚洲精品线观看动态图| 亚洲人片在线观看天堂无码| 久久免费看黄a级毛片| 国产偷v国产偷v亚洲高清| 久久国产乱子伦精品免费强| 国产精品亚洲高清一区二区| 91在线视频免费观看| 亚洲精品天堂成人片?V在线播放| 亚洲综合激情五月色一区| 99免费观看视频| 亚洲AV中文无码字幕色三| 巨波霸乳在线永久免费视频 | 青青操视频在线免费观看| 久久久久亚洲AV无码网站| 久章草在线精品视频免费观看| 国产啪亚洲国产精品无码| 最近最新高清免费中文字幕 | 人人狠狠综合久久亚洲88| 韩国免费a级作爱片无码| 中文字幕亚洲色图| 久久w5ww成w人免费|