简体   繁体   English

从LDAP获取组和用户

[英]Get groups and users from LDAP

Hi i am trying to fetch all posixGroup from LDAP and the users in this group. 嗨,我正在尝试从LDAP和该组中的用户获取所有posixGroup。 Code below is what i have done so far, it returns me all the groups but i am not sure how to get the users for these groups. 下面的代码是我到目前为止所做的事情,它返回了我所有的组,但是我不确定如何获得这些组的用户。 Please guide me is this approach good? 请指导我这种方法好吗? or should i go by first getting the users and then based on GID get the group name? 还是我应该先获取用户,然后基于GID获取组名?

public static void main(String[] args) {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL,"ldap://192.168.*.*:389");
        env.put(Context.URL_PKG_PREFIXES, "com.sun.jndi.url"); 
        env.put(Context.REFERRAL, "ignore"); 
        env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
        env.put(Context.SECURITY_PRINCIPAL, "cn=manager,dc=*,dc=*"); 
        env.put(Context.SECURITY_CREDENTIALS, "****");

        DirContext ctx;
        try {
            ctx = new InitialDirContext(env);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }

        NamingEnumeration results = null;
        try {

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

            results = ctx.search("ou=path,dc=*,dc=*", "(objectClass=posixGroup)",controls);
            // Go through each item in list
            while (results.hasMore()) {
                SearchResult nc = (SearchResult)results.next();
                Attributes att=     nc.getAttributes();                           
                System.out.println("Group Name "+ att.get("cn").get(0));
                System.out.println("GID "+ att.get("GidNumber").get(0));
            }
        } catch (NameNotFoundException e) {
            System.out.println("Error : "+e);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception e) {
                    System.out.println("Error : "+e);
                }
            }
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception e) {
                    System.out.println("Error : "+e);
                }
            }
        }      

    }

Querying all users in a group 查询组中的所有用户

It depends which attribute is used by groups in your directory to denote membership. 它取决于目录中的组使用哪个属性表示成员身份。 posixGroup uses memberUid with the username as value (defined in RFC 2307 ). posixGroup使用memberUid和用户名作为值(在RFC 2307中定义)。 There are other possible attributes (member, uniquemember) and values (DN) so check what your directory uses. 还有其他可能的属性(成员,唯一成员)和值(DN),因此请检查目录使用的内容。

So in order to load all users from a group, you would have to: 因此,为了从组中加载所有用户,您必须:

  1. Query that group, for example with this filter (&(objectClass=posixGroup)(cn=<group name>)) 使用该过滤器查询该组(&(objectClass=posixGroup)(cn=<group name>))
  2. Iterate through all values of memberUid in the group, for each: 依次遍历组中memberUid所有值:
    1. Query the user object with (&(objectClass=posixAccount)(uid=<memberUid>)) 使用(&(objectClass=posixAccount)(uid=<memberUid>))查询用户对象
    2. Then you can access user attributes like uidNumber . 然后,您可以访问用户属性,例如uidNumber

This is not a very efficient way of doing it because it will generate lots of small queries, but as far as I know, LDAP has no means to join a group entry with the user entries it references in a single result (unlike SQL). 这不是一种非常有效的方法,因为它会生成许多小的查询,但是据我所知,LDAP无法将组条目与其在单个结果中引用的用户条目联接在一起(不同于SQL)。

You could optimise it a bit by limiting results to the attributes you actually need: gidNumber for the group query and uidNumber for the user query. 您可以通过将结果限制为实际需要的属性来对它进行一些优化:对组查询使用gidNumber ,对用户查询使用uidNumber Using either SearchControls.setReturningAttributes() or a version of DirContext.search() that takes a attributesToReturn argument. 使用SearchControls.setReturningAttributes()或带有attributesToReturn参数的DirContext.search()版本。 It doesn't reduce the number of queries though, only the volume of data returned. 但是,它不会减少查询数量,只会减少返回的数据量。

Some more general notes about LDAP usage 有关LDAP使用的一些更一般的说明

  • If your queries have a large number of results (for example "all users"), you might hit your directory's result size limit (typically 5000) and only get partial results. 如果查询包含大量结果(例如“所有用户”),则可能会达到目录的结果大小限制(通常为5000),并且只会得到部分结果。
  • When modifying group membership information, you have to update both posixAccount and posixGroup objects (unless your directory server does it, but I doubt it will), otherwise it becomes inconsistent. 修改组成员资格信息时,必须同时更新posixAccountposixGroup对象(除非目录服务器执行此操作,但我怀疑posixAccount ),否则它将变得不一致。

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

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