【问题标题】:Find BASE DN from LDAP directory context object从 LDAP 目录上下文对象中查找 BASE DN
【发布时间】:2011-10-15 09:47:43
【问题描述】:


我有 LDAP 的目录上下文,但我需要从中找出 BASE DN 目录上下文对象。 我有以下代码来获取目录上下文对象,

// Configure our directory context environment.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://test.mycomp.com:389");
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
env.put(Context.SECURITY_PRINCIPAL,"uid=test.gen,OU=Generics,O=test.mycomp.com");
env.put(Context.SECURITY_CREDENTIALS, "test123");
DirContext dirContext = new InitialDirContext(env);
System.out.println("loaded dirContext");

我有以下代码来获取基本 DN, 我一直在返回基本 DN 名称,但我想优化我的过滤器而不是放置 2 个循环来获取基本 DN,

    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
    NamingEnumeration results = dirContext.search("",
            "(&(objectClass=organization)(objectClass=top))", constraints);

        // Fail if no entries found
    if (results == null || !results.hasMore()) {
        System.out.println("No result found");
        return;
    }

    while(results.hasMoreElements()){
        Object res = results.next();
        SearchResult serResult = (SearchResult) res;
        Attributes atts = serResult.getAttributes();
        System.out.println(atts.toString());
        Attribute baseAttr = atts.get("namingContexts");
        NamingEnumeration  ids = baseAttr.getAll();
        while(ids.hasMoreElements()){
            Object obj = ids.next();
            System.out.println(obj.toString());
        }
    }

请帮我优化我的过滤器。

【问题讨论】:

    标签: java ldap ldap-query


    【解决方案1】:

    您不需要搜索。只需从 InitialContext 中获取 NamingContexts 属性即可。

    Attributes atttrs = context.getAttributes("", new String[]{"namingContexts"});
    

    【讨论】:

    • 听起来很简单,但我该怎么做呢?
    • 属性 atttrs = context.getAttributes("", new String[]{"namingContexts"});
    • 我们怎样才能找到子目录呢?
    【解决方案2】:

    当查询根 DSE 时,符合 LDAP 的目录服务器应提供有关 namingContexts 的信息。有关根 DSE 的详细信息,请参阅“LDAP: The root DSE”。 UnboundID LDAP SDK 提供了一个封装根 DSE 的类和一个检索它的便捷方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多