2013-02-21 64 views
2

我有一個搜索LDAP的問題。如果我使用下面的代碼,我可以使用下面的代碼獲得2級。 但我想獲得4級對象。感謝您的任何幫助。LDAP JNDI子樹搜索

當前的搜索基礎:ou=HQ2-BR,過濾器:"(ou=*)";

問候, 樸文香,戴夫 [email protected] [email protected]

LDAP結構

  • o = com,dc = rabbitforever#(等級0)
    • ou = HQ2-BR // referal其他廣告#(水平1)
      • OU = TSB //#(級別2)
      • OU = BM1 //#(2級)
      • OU = IIC //#(級別2)
        • OU =人們//#(3級)
          • UID = IICCIO //#(4級)
          • UID = IICSIO1 //#(4級)

代碼:

public void loopLDAP() { 
    String adminName = "uid=writer,ou=People,o=com,dc=rabbitforever"; 
    String adminPassword = "password"; 

    Properties env = new Properties(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
      "com.sun.jndi.ldap.LdapCtxFactory"); 
    //env.put(Context.PROVIDER_URL, 
    //  "ldap://192.168.1.127:389/dc=rabbitforever,dc=com"); 
    env.put(Context.PROVIDER_URL, 
      "ldap://10.10.176.156:389/o=com,dc=rabbitforever"); 
    //env.put(Context.SECURITY_AUTHENTICATION, "none"); 

    env.put(Context.SECURITY_PRINCIPAL, adminName); 
    env.put(Context.SECURITY_CREDENTIALS, adminPassword); 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    env.put(Context.REFERRAL, "follow"); 

    try { 
     LdapContext ctx = new InitialLdapContext(env, null); 
     ctx.setRequestControls(null); 

     String filter = "(ou=*)"; 

     NamingEnumeration<?> namingEnum = ctx.search("ou=HQ2-BR", filter, 
       getSimpleSearchControls()); 
     while (namingEnum.hasMore()) { 
      SearchResult result = (SearchResult) namingEnum.next(); 
      Attributes attrs = result.getAttributes(); 

      String cn = ""; 
      String sn = ""; 
      String description = ""; 
      String uid = ""; 
      if (null != attrs.get(cn)) { 
       cn = attrs.get("cn").toString(); 
      } 
      if (null != attrs.get("sn")) { 
       sn = attrs.get("sn").toString(); 
      } 
      if (null != attrs.get("description")) { 
       description = attrs.get("description").toString(); 
      } 
      if (null != attrs.get("uid")) { 
       uid = attrs.get("uid").toString(); 
      } 
      System.out.println(cn + " | " + sn + " | " + description 
        + " | " + uid); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} // end loopLDAP() 

回答

2

你可能需要構建SearchControls物體SearchControls.SUBTREE_SCOPE,並將其傳遞給ctx.search方法。從另一個答案看example