简体   繁体   中英

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. How do I go about JUST extracting/returning the employee numbers only (for all users within the filter)

Thanks

@Sampisa's solution was correct:

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"))
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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