2

我在搜索 LDAP 时遇到问题。如果我使用以下代码,我可以使用以下代码获得级别 2。但我想获得 4 级对象。感谢您提供任何帮助。

当前搜索库:ou=HQ2-BR,过滤器:"(ou=*)";

问候, Man Pak Hong, Dave manpakhong@hotmail.com manpakhong@gmail.com

LDAP 结构

  • o=com,dc=rabbitforever #(0 级)
    • ou=HQ2-BR // 引用其他广告#(level 1)
      • ou=TSB // #(2级)
      • ou=BM1 // #(2级)
      • ou=IIC // #(2级)
        • ou=People // #(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()
4

1 回答 1

2

您可能需要用 构造SearchControls对象SearchControls.SUBTREE_SCOPE,并将其传递给ctx.search方法。请参阅另一个答案的示例

于 2013-03-25T15:55:13.087 回答