从LDAP目录上下文对象中找到BASE DN [英] Find BASE DN from LDAP directory context object

查看:808
本文介绍了从LDAP目录上下文对象中找到BASE DN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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


I have directory context for LDAP but i need to find out the BASE DN from that directory context object. I have following code to get Directory context object,

// 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,

I have following code to get the Base DN, I has been returning base DN name but i want to make my filter optimised rather than putting 2 loops to get base 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());
        }
    }

请帮助我优化我的过滤器.

Please help me out to optimize my filter.

推荐答案

您不需要搜索.只需从InitialContext获取namingContexts属性即可.

You don't need the search. Just get the namingContexts attribute from the InitialContext.

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

这篇关于从LDAP目录上下文对象中找到BASE DN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆