V
V
Vanes Ri_Lax2015-09-18 14:45:37
Java
Vanes Ri_Lax, 2015-09-18 14:45:37

Why does AD user search not work?

Hello, it is necessary to implement a program that will look for a user in AD.
I found an example here
I redid it for myself, here's what I got:

package ru.domen;

import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;



class TestAD 
{ 
  static DirContext ldapContext; 
  public static void main (String[] args) throws NamingException 
  { 
    try 
    { 

      
        String userName = "test1";
        String passWord = "123467";
        String base = "OU=Клиенты,DC=domen,DC=ru";
        String dn = "uid=" + userName + "," + base;
        String ldapURL = "ldap://host:389/";
        ldapURL += base;

          Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11);
          ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
; 
          ldapEnv.put(Context.PROVIDER_URL, ldapURL);
          ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
          
          ldapEnv.put(Context.SECURITY_PRINCIPAL, userName);
          ldapEnv.put(Context.SECURITY_CREDENTIALS, passWord);

          //ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrateur,cn=users,dc=societe,dc=fr"); 
         // ldapEnv.put(Context.SECURITY_PRINCIPAL, "OU=Клиенты,DC=domen,DC=ru");
         // ldapEnv.put(Context.SECURITY_CREDENTIALS, "pwd");
      //ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl"); 
          //ldapEnv.put(Context.SECURITY_PROTOCOL, "simple"); 
          ldapContext = new InitialDirContext(ldapEnv);

          // Create the search controls          
          SearchControls searchCtls = new SearchControls();
          
          
          

          //Specify the attributes to return 
          String returnedAtts[] = {"sn", "givenName", "sAMAccountName"};
          searchCtls.setReturningAttributes(returnedAtts);

          //Specify the search scope 
         searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

          //specify the LDAP search filter 
          String searchFilter = "sAMAccountName=5836";

          //Specify the Base for the search 
          String searchBase = "OU=Клиенты,DC=domen,DC=ru";
          //initialize counter to total the results 
          int totalResults = 0;

          // Search for objects using the filter 
          NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls);

          //Loop through the search results 
          while (answer.hasMoreElements()) {
              SearchResult sr = (SearchResult) answer.next();

              totalResults++;

              System.out.println(">>>" + sr.getName());
              Attributes attrs = sr.getAttributes();
              System.out.println(">>>>>>" + attrs.get("sAMAccountName"));
          }

          System.out.println("Total results: " + totalResults);
        
        
          ldapContext.close();
        
        
      } catch (Exception e) {
          //System.out.println(" Search error: " + e);
          e.printStackTrace();
          System.exit(-1);
      }
  } 
    
    
   
}

Here is what happens after running the program:
javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-03100238, problem 2001 (NO_OBJECT), data 0, best match of:
  'OU=Клиенты,DC=domen,DC=ru'
]; remaining name 'OU=Клиенты,DC=domen,DC=ru'
  at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3160)
  at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3081)
  at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2888)
  at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1846)
  at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1769)
  at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:392)
  at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:358)
  at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:341)
  at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:267)
  at ru.domen.TestAD.main(TestAD.java:75)
Java Result: -1

What am I doing wrong, what could be the problem?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YV, 2015-09-18
@targetjump

The filter is incorrect, do this:
+ you can add an ObjectClass to the filter

(&(sAMAccountName=5836)(objectClass=inetOrgPerson)) // ну или какой там у вас objectClass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question