简体   繁体   English

如何为LDAP Java中的所有用户返回特定属性?

[英]How to return specific attributes for all users in LDAP Java?

I am trying to return employee numbers of everyone in search filter 我正在尝试返回搜索过滤器中每个人的员工编号

(&(employeeType= Workforce)(objectClass=person))

This is my code: 这是我的代码:

import java.util.Enumeration;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import java.util.Hashtable;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class testing2 {
    public testing2() {

    }

    public void doLookup() {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        properties.put(Context.PROVIDER_URL, "removed for reasons");
        properties.put(Context.SECURITY_AUTHENTICATION,"simple");
        properties.put(Context.SECURITY_PRINCIPAL,"removed"); 
        properties.put(Context.SECURITY_CREDENTIALS,"secret");
        try {
            DirContext context = new InitialDirContext(properties);
            SearchControls searchCtrls = new SearchControls();
            searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String filter = "(&(employeeType= Workforce)(objectClass=person))";
            NamingEnumeration values = context.search("o = xyz",filter,searchCtrls);
            while (values.hasMoreElements())
            {
                SearchResult result = (SearchResult) values.next();
                Attributes attribs = result.getAttributes();

                if (null != attribs)
                {
                    for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
                    {
                        Attribute atr = (Attribute) ae.next();
                        String attributeID = atr.getID();
                        for (Enumeration vals = atr.getAll(); 
                            vals.hasMoreElements(); 
                            System.out.println(attributeID +": "+ vals.nextElement()));
                            //System.out.println(attributeID.getAttributes().get("uid"))
                    }
                }
            }

            context.close();

        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        testing2 sample = new testing2();
        sample.doLookup();
    }

}

The thing is that when I run this, I get everything/all information about all the users - including attributes like first name, last name, title, givenName, postal code, employee number and more. 问题是,当我运行此命令时,我会获得有关所有用户的所有信息/所有信息-包括诸如名字,姓氏,职务,givenName,邮政编码,员工编号等属性。 How do I go about JUST extracting/returning the employee numbers only (for all users within the filter) 我该如何只提取/返回员工编号(对于过滤器中的所有用户)

Thanks 谢谢

@Sampisa's solution was correct: @Sampisa的解决方案是正确的:

if (null != attribs) {
    Attribute myattrib = attribs.get("employeeNumber");
    System.out.println(myattrib); // or myattrib.get(0)
 }

instead of 代替

 if (null != attribs)
            {
                for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
                {
                    Attribute atr = (Attribute) ae.next();
                    String attributeID = atr.getID();
                    for (Enumeration vals = atr.getAll(); 
                        vals.hasMoreElements(); 
                        System.out.println(attributeID +": "+ vals.nextElement()));
                        //System.out.println(attributeID.getAttributes().get("uid"))
                }
            }

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

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