隨筆-86  評論-33  文章-0  trackbacks-0

           

          import java.util.Properties;
          import javax.naming.*;
          import javax.naming.ldap.*;
          import javax.naming.directory.*;
          public class Client {
          public static void main (String[] args) {
              Properties env 
          = new Properties();
              String adminName 
          = "usish\\derek";
              String adminPassword 
          = "dada";
              String ldapURL 
          = "LDAP://192.168.40.42:389";
              env.put(Context.INITIAL_CONTEXT_FACTORY, 
          "com.sun.jndi.ldap.LdapCtxFactory");
          //set security credentials, note using simple cleartext authentication
              env.put(Context.SECURITY_AUTHENTICATION, "simple");
              env.put(Context.SECURITY_PRINCIPAL, adminName);
              env.put(Context.SECURITY_CREDENTIALS, adminPassword);
          //connect to my domain controller
              env.put(Context.PROVIDER_URL, ldapURL);

              
          try {
          //Create the initial directory context
                  LdapContext ctx = new InitialLdapContext(env, null);

          //Create the search controls
                  SearchControls searchCtls = new SearchControls();

          //Specify the search scope
                  searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
          //specify the LDAP search filter
                  String searchFilter = "(&(objectCategory=person)(objectClass=user)(name=004*))";

          //Specify the Base for the search
                  String searchBase = "OU=USISH,DC=usish,DC=com,DC=cn";
          //initialize counter to total the group members
                  int totalResults = 0;
          //Specify the attributes to return
                  String returnedAtts[] = {"memberOf"};
                  searchCtls.setReturningAttributes(returnedAtts);

          //Search for objects using the filter
                  NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                                                        searchCtls);
          //Loop through the search results
                  while (answer.hasMoreElements()) {
                      SearchResult sr 
          = (SearchResult) answer.next();
                      System.out.println(
          ">>>" + sr.getName());
                      
          //Print out the groups
                      Attributes attrs = sr.getAttributes();
                      
          if (attrs != null) {
                          
          try {
                              
          for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) {
                                  Attribute attr 
          = (Attribute) ae.next();
                                  System.out.println(
          "AttributeID: " + attr.getID());
                                  
          for (NamingEnumeration e = attr.getAll(); e.hasMore();
                                          totalResults
          ++) {
                                      System.out.println(
          "Attributes:"+e.next());                            
                                  }
                              }
                          } 
          catch (NamingException e) {
                              e.printStackTrace();
                              System.err.println(
          "Problem listing membership: " + e);
                          }
                      }
                  }
                  System.out.println(
          "Total groups: " + totalResults);
                  ctx.close();
              }
          catch (NamingException e) {
                  e.printStackTrace();
                  System.err.println(
          "Problem searching directory: " + e);
              }
           }
          }
          posted on 2007-05-09 15:12 Derek.Guo 閱讀(2296) 評論(1)  編輯  收藏 所屬分類: Java

          評論:
          # re: JAVA訪問LDAP[未登錄] 2008-09-28 13:22 | ice
          我在運上面的代碼出現了 cuold not find the main class.Program will exit! 問題怎么解決啊 ,能幫幫我嗎,我是第一遇到這個問題

            回復  更多評論
            
          MSN:envoydada@hotmail.com QQ:34935442
          主站蜘蛛池模板: 建始县| 唐河县| 宜兰县| 武安市| 手游| 西和县| 沙田区| 韩城市| 鄢陵县| 安义县| 涿鹿县| 秀山| 安丘市| 沁水县| 凤凰县| 闽侯县| 芦山县| 松原市| 梅州市| 鄂温| 公主岭市| 东光县| 抚顺市| 樟树市| 蒲城县| 额敏县| 阿尔山市| 潮州市| 永平县| 囊谦县| 保亭| 通海县| 阿合奇县| 望奎县| 广宗县| 汉阴县| 公安县| 安陆市| 塔河县| 天津市| 济源市|