簡體   English   中英

從LDAP目錄上下文對象中找到BASE DN

[英]Find BASE DN from LDAP directory context object


我有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());
        }
    }

請幫助我優化我的過濾器。

您不需要搜索。 只需從InitialContext獲取namingContexts屬性。

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

當查詢根DSE時,符合LDAP的目錄服務器應提供有關namingContexts信息。 有關根DSE的更多信息,請參閱“ LDAP:根DSE ”。 UnboundID LDAP SDK提供了一個用於封裝根DSE的類和一個方便的方法來檢索它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM