java - 使用 Java 从 LDAP 检索所有用户及其角色

标签 java ldap openldap ldap-query ldapconnection

我有一个 Web 应用程序。对于 LDAP,我使用的是 Apache Directive Studio。 我想在我的应用程序中获取所有用户及其角色。

我可以使用以下代码获取特定信息。

    import java.util.Properties;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;

    public class DirectorySample {
        public DirectorySample() {

        }

        public void doLookup() {
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY,
                    "com.sun.jndi.ldap.LdapCtxFactory");
            properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
            try {
                DirContext context = new InitialDirContext(properties);
                Attributes attrs = context.getAttributes("dc=example,dc=com");
                System.out.println("ALL Data: " + attrs.toString());
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            DirectorySample sample = new DirectorySample();
            sample.doLookup();
        }

    }

enter image description here
我想显示所有用户和角色列表,所以我需要更改查询或其他内容 提前致谢。

最佳答案

您可以使用 org.apache.directory.ldap.client.api.LdapConnection 来轻松搜索。

绑定(bind)连接后,在连接上进行搜索。遍历光标以获取所需的对象。第一个参数应该与您的用户父级的 DN 相匹配。下面的例子只是给你一个想法。

EntryCursor cursor = connection.search( "ou=users, dc=example, dc=com", "(objectclass=*)", SearchScope.ONELEVEL, "*" );

    while ( cursor.next() )
    {
        Entry entry = cursor.get();
            //play with the entry
    }

关于java - 使用 Java 从 LDAP 检索所有用户及其角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21596562/

相关文章:

java - 如何使用 Polygon 类在 libGdx 中绘制六边形 map ?

java - 可滚动的 vaadin View 以显示页面中的所有组件

ubuntu - Ubuntu 11.04 中的 OpenLDAP memberof Overlay 配置

java - OpenLDAP 和 SSL

c - 如何使用 Msys2 和 MinGW 在 Windows 上构建 OpenLDAP 库?

java - 从 fragment 调用 Activity 使用接口(interface),但是为什么呢?

java - spymemcached(Java Memcached 客户端)

ldap - SonarQube 5.2,LDAP 插件 1.5 : com4j. ComException

LDAP测试环境

LDAP 查询 : ldapsearch for uniqueMember