<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 閱讀(1388) 評論(1)  編輯  收藏 所屬分類: JNDI

    FeedBack:
    # re: 用jdni 對LDAP讀寫操作代碼
    2007-11-18 11:20 | 雪浪
    謝謝!!!  回復  更多評論
      
    主站蜘蛛池模板: 亚洲日韩国产精品乱-久| 在线观看亚洲av每日更新| 亚洲精品视频免费在线观看| 国产午夜无码精品免费看动漫| 免费一级毛片不卡不收费| 久久亚洲AV成人无码国产电影 | 一区二区三区四区免费视频 | 国产日韩久久免费影院| 最新亚洲春色Av无码专区| 国产在线jyzzjyzz免费麻豆| 亚洲视频手机在线| 亚洲黄色免费网址| 免费人妻av无码专区| 国产精品亚洲一区二区无码| 男人的天堂亚洲一区二区三区 | 最近中文字幕无吗免费高清 | 亚洲日本va中文字幕久久| 亚洲国产精品线观看不卡| 国产人成免费视频网站| 中文字幕在线日亚洲9| 国产精品国产自线拍免费软件| 亚洲电影国产一区| 亚洲AV无码专区在线电影成人| 在线观看国产情趣免费视频| 亚洲av无码精品网站| 在线观看亚洲网站| 久久久久亚洲AV无码专区桃色| 亚洲乱妇老熟女爽到高潮的片| 色欲A∨无码蜜臀AV免费播| 亚洲综合在线成人一区| 免费毛片在线看片免费丝瓜视频| 亚洲6080yy久久无码产自国产| 亚洲视频一区二区| 免费一区二区三区在线视频| 亚洲日韩精品A∨片无码| 18女人毛片水真多免费| 亚洲av日韩综合一区二区三区| 中文字幕亚洲第一| 免费下载成人电影| 四虎影视永久在线精品免费| 久久精品亚洲日本佐佐木明希|