简体   繁体   English

从NamingEnumeration中获取元素

[英]Getting elements out of a NamingEnumeration

I'm trying to get the elements out of a namingenumeration. 我试图从namingenumeration中获取元素。 The namingenumeration itself is not null, but hasNext() gives me false. namingenumeration本身不是null,但hasNext()给我假。

What am I doing wrong? 我究竟做错了什么?

public static void main(String[] args) {
try {
            DirContext context = new InitialDirContext(
                    Environment.getEnvironment());



            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
             String[] attrIDs = { "cn", "givenname", "sn", "mail" };
             controls.setReturningAttributes(attrIDs);
            NamingEnumeration enumResult = context.search(
                    "DC=PORTAL,DC=COMPANY,DC=BE", "(CN=*)",
                    controls);
            System.out.println(enumResult.hasMore());

            context.close();

        } catch (AuthenticationException e) {
            System.out.println("Invalid credentials");
        } catch (NamingException e) {
            System.out.println("Lookup failed: " + e);
        }
}

Structure of AD (on Localhost using AD-LDS) AD的结构(使用AD-LDS在Localhost上)

DC=PORTAL,DC=COMPANY,DC=BE DC =门户网站,DC = COMPANY,DC = BE
->OU=Accounts - > OU =帐户
==>CN=John Doe ==> CN = John Doe
==>CN=Jane Doe ==> CN = Jane Doe
->CN=LostAndFound - > CN = LostAndFound
->CN=NTDS Quotas - > CN = NTDS配额
->CN=Roles - > CN =角色
->OU=System Accounts - > OU =系统帐户
==>CN=PortalAdmin ==> CN = PortalAdmin

目录结构

Narrowing my searchbase to "OU=ACCOUNTS,DC=PORTAL,DC=COMPANY,DC=BE" gives the following error 将我的搜索库缩小为“OU = ACCOUNTS,DC = PORTAL,DC = COMPANY,DC = BE”会出现以下错误

Lookup failed: javax.naming.NameNotFoundException: [LDAP: error code 32 - 000020 8D: NameErr: DSID-031522C9, problem 2001 (NO_OBJECT), data 0, best match of: 'DC=PORTAL,DC=COMPANY,DC=BE' ]; 查找失败:javax.naming.NameNotFoundException:[LDAP:错误代码32 - 000020 8D:NameErr:DSID-031522C9,问题2001(NO_OBJECT),数据0,最佳匹配:'DC = PORTAL,DC = COMPANY,DC = BE ']; remaining name 'OU=ACCOUNTS,DC=PORTAL,DC=COMPANY,DC=BE' 剩余名称'OU = ACCOUNTS,DC = PORTAL,DC = COMPANY,DC = BE'


solution: 解:

try {

            DirContext ctx = new InitialDirContext(Environment.getEnvironment());



            // Get all the attributes of named object
            Attributes attrs = ctx
                    .getAttributes("cn=John Doe,ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE");

            if (attrs == null) {
                System.out.println("No attributes");
            } else {
                /* Print each attribute */
                try {
                    for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
                        Attribute attr = (Attribute) ae.next();
                        System.out.println("attribute: " + attr.getID());

                        /* print each value */
                        for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out
                                .println("value: " + e.next()))
                            ;
                    }
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }



            ctx.close();

        } catch (AuthenticationException e) {
            System.out.println("Invalid credentials");
        } catch (NamingException e) {
            System.out.println("Lookup failed: " + e);
        }

Start with doing some basic sanity checks. 从做一些基本的健全性检查开始。 For instance, that the data returned by Environment.getEnvironment() is correct (url, port, user, password) and allows a connection to the directory server. 例如, Environment.getEnvironment()返回的数据是正确的(url,port,user,password),并允许连接到目录服务器。 Also check that there are no network problems and that you can, in fact, access the server. 还要检查是否存在网络问题,并且您实际上可以访问服务器。

Try limiting the search base a bit more, for instance: "OU=Accounts,DC=PORTAL,DC=COMPANY,DC=BE" and see if some results are returned. 尝试稍微限制搜索基础,例如:“OU = Accounts,DC = PORTAL,DC = COMPANY,DC = BE”并查看是否返回了一些结果。 Also check if the objects in the expected results actually have the attributes "cn", "givenname", "sn", "mail". 还要检查预期结果中的对象是否实际具有属性“cn”,“givenname”,“sn”,“mail”。

Other than that, there are no obvious mistakes in the code shown in the question, it should work fine. 除此之外,问题中显示的代码没有明显的错误,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM