简体   繁体   中英

Unable to retrieve Active Directory users from Java backend using LDAP connection [LDAP: error code 32 – No Such Object]

I would like to connect to an AD and filter by user to check if it exists (uid). In the future I will check the password too.

For now I have tried to implement something as simple as list all the users, their surnames and their id on the screen but it has not worked. The goal is to get a list with all the uid and check if the same uid exists in my website database.

I got this error: LDAP: error code 32 – No Such Object

It is the first time that I work with AD and I think I am doing something wrong with the AD Tree.

Here is the code with what I have tried:

public class ActiveDirectory {

    private Properties properties;
    private DirContext dirContext;
    private boolean conected = false;
    private String Error;


    public ActiveDirectory(String username, String password, String domainController) {

        //Path keystore whith the registred SSL certficate
        String keystorePath = "C:\\Program Files\\Java\\jdk-12.0.2\\lib\\security\\cacerts";
        System.setProperty("javax.net.ssl.keyStore", keystorePath);


        System.setProperty("javax.net.ssl.keyStorePassword", "******");

        properties = new Properties();        
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        properties.put(Context.SECURITY_AUTHENTICATION,"simple");

        properties.put(Context.PROVIDER_URL, "LDAPS://kldap.***.***:636");
        properties.put(Context.SECURITY_PRINCIPAL, "uid=blabla,ou=blabla,DC=blabla,DC=blabla");
        properties.put(Context.SECURITY_CREDENTIALS, "******");

        //Initializing active directory LDAP connection
        try {
                    dirContext = new InitialDirContext(properties);
                    String searchFilter = "(objectClass=inetOrgPerson)";
                    String[] requiredAttributes= {"sn", "cn", "employeeNumber"};
                    SearchControls controls = new SearchControls();
                    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    controls.setReturningAttributes(requiredAttributes);
                    NamingEnumeration users = dirContext.search("ou=Users,o=Company", searchFilter, controls);
                    SearchResult searchResult = null;
                    String commonName = null;
                    String surName = null;
                    String employeeNum = null;
                    while (Users.hasMore()) {
                           searchResult = (SearchResult) Users.next();
                           Attributes attr = searchResult.getAttributes();
                           commonName = attr.get("cn").get(0).toString();
                           surName = attr.get("sn").get(0).toString();
                           employeeNum = attr.get("employeeNumber").get(0).toString();
                           System.out.println("Name: " + commonName);
                           System.out.println("Surname: " + surName);
                           System.out.println("Employee number = " + employeeNum);
                    }
                    conected = true;
             } catch (NamingException e) {
                    conected = false;
                    Error = e.getMessage();
                    LOG.severe(e.getMessage());
                    e.printStackTrace();
             }
    }
       public boolean isConected() {
             if (conected) {
                    return true;
             }else {
                    return false;
             }
       }
       public String getError() {
             return Error;
       }
}

LDAP 树

Solved!

This is wrong: NamingEnumeration users = dirContext.search("ou=Users,o=Company", searchFilter, controls);

The correct approach was:

NamingEnumeration users = dirContext.search("ou=Users,DC=Blabla,DC=Blabla", searchFilter, controls);

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