隨筆-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 閱讀(2303) 評論(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
          主站蜘蛛池模板: 祁连县| 新余市| 灌云县| 永康市| 墨竹工卡县| 五寨县| 平果县| 卓资县| 海丰县| 永靖县| 班戈县| 南木林县| 奉贤区| 公安县| 松滋市| 滁州市| 准格尔旗| 界首市| 凉城县| 保亭| 台江县| 全州县| 山阳县| 上犹县| 内乡县| 上杭县| 东乌| 东明县| 临漳县| 马关县| 札达县| 高陵县| 黑龙江省| 镇远县| 中西区| 郓城县| 石门县| 台北市| 阳高县| 镇江市| 闵行区|