2014-02-06 51 views
4

我有一个Web应用程序。对于LDAP,我正在使用Apache指令工作室。 我想让我的应用程序中的所有用户和他们的角色。使用Java从LDAP检索所有用户及其角色

我能够通过使用以下代码获取特定信息。

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
我想告诉所有的用户和角色列表,所以我需要改变查询或其他 感谢东西进展。

+0

(1)您发布的代码没有做任何检索特定用户数据的事情。它检索“dc = example,dc = com”的属性,根本不是用户条目。 (2)检索所有用户的数据是一个潜在的巨大查询。你为什么认为你需要这样做? – EJP

+0

好吧..我想要所有用户和角色。 你可以建议我查询...对于@EJP –

+0

查询取决于你如何定义你的DIT,你已经提供了零信息。例如,你为用户使用了什么objectClass? – EJP

回答

1

您可以使用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 
    } 
相关问题