繁体   English   中英

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

[英]Retrieve all users and their roles from LDAP using Java

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

    }

在此输入图像描述
我想显示所有用户和角色列表,所以我需要更改查询或其他感谢提前。

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

绑定连接后,请搜索连接。 循环光标以获取所需的对象。 第一个参数应与用户父级的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
    }

暂无
暂无

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

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