java - LDAP:使用Java获取目录中包含所有用户信息的列表

标签 java ldap

我需要获取包含目录中所有用户信息的列表。我正在尝试通过以下代码来获取:

        DirContext ctx = new InitialDirContext(env);

        boolean ignoreCase = true;
        Attributes matchAttrs = new BasicAttributes(ignoreCase);
        matchAttrs.put( new BasicAttribute("") );

        //LDAP_Attributes : Attributes of every user e.g. name, phone# etc.
        NamingEnumeration answer = ctx.search( "ou=People", matchAttrs, LDAPUser.LDAP_ATTRIBUTES );

由于安全限制,我无法访问 LDAP 服务器,因此无法对其进行测试。请建议上述方法是否正确。谢谢!

最佳答案

检查下面的代码示例以使用 Search() 获取信息。我想这可能对你有帮助。

AllSearch.java

package usingj2ee.naming;

import javax.naming.*;
import javax.naming.directory.*;

public class AllSearch
{
    public static void main(String[] args)
    {
        try
        {
// Get the initial context
            InitialDirContext ctx = new InitialDirContext();

            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Search for items with the specified attribute starting
// at the top of the search tree
            NamingEnumeration objs = ctx.search(
                "ldap://ldap.wutka.com/o=Wutka Consulting, dc=wutka, dc=com",
                "(objectClass=*)", searchControls);

// Loop through the objects returned in the search
            while (objs.hasMoreElements())
            {
// Each item is a SearchResult object
                SearchResult match = (SearchResult) objs.nextElement();

// Print out the node name
                System.out.println("Found "+match.getName()+":");

// Get the node's attributes
                Attributes attrs = match.getAttributes();

                NamingEnumeration e = attrs.getAll();

// Loop through the attributes
                while (e.hasMoreElements())
                {
// Get the next attribute
                    Attribute attr = (Attribute) e.nextElement();

// Print out the attribute's value(s)
                    System.out.print(attr.getID()+" = ");
                    for (int i=0; i < attr.size(); i++)
                    {
                        if (i > 0) System.out.print(", ");
                        System.out.print(attr.get(i));
                    }
                    System.out.println();
                }
                System.out.println("---------------------------------------");
            }
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}

Referenced from here

关于java - LDAP:使用Java获取目录中包含所有用户信息的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540633/

相关文章:

java - 写一个算法来找到最大值

Spring LDAP 示例需要持久性吗?

ldap - chain_provider和身份验证提供程序

ldap - "Choice"的 BER 编码

python - LDAP:ldap.SIZELIMIT_EXCEEDED

java - 我对java完全陌生,想知道是否有任何方法可以改进这个程序

java - Groovy 冗余修剪() 调用

java - 正则表达式 : Insert space in the string after a matched pattern

java - Android SwipeTabs 之后 ImageView 发生变化

ssl - Glassfish 通过 SSL 连接到 LDAP 时延迟 30 秒