Recuperar e listar usuarios do LDAP com java

1 resposta
S

olá galera!

estou com um problema, estou tentando recuperar uma lista de usuarios do LDAP, mas esta occorendo o seguinte erro:
"
Exception in thread “main” javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece]; remaining name’CN=meunome,OU=meugrupo,OU=empresa,DC=corporate,DC=group’ "

estou usando a seguinte classe:

package test;

import java.util.Hashtable;

import javax.naming.Context;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;

import javax.naming.directory.Attributes;

import javax.naming.directory.SearchControls;

import javax.naming.directory.SearchResult;

import javax.naming.ldap.InitialLdapContext;

import javax.naming.ldap.LdapContext;

public class DadosLoginActiveDirectory{

public static void main(String[] args) throws NamingException {   

    String loginPesquisado = "meuLogin";   
    // specify the LDAP search filter   
    String searchBase = "DC=corporate,DC=group";   
    // prepara parametros de conexao ao AD   
    Hashtable<String, String> envDC = new Hashtable<String, String>();   
    envDC.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");   
     // atribui as credenciais de segurança (note: using simple cleartext   
    // authentication)   
    envDC.put(Context.SECURITY_AUTHENTICATION, "simple");   
    // usuario para fazer o bind com o AD   
    envDC.put(Context.SECURITY_PRINCIPAL, "meu usuario"); //<usuario+dominio>);   
       envDC.put(Context.SECURITY_CREDENTIALS, "minha senha"); // <senha>);   
    // URL de conexao   
    envDC.put(Context.PROVIDER_URL, "LDAP://urldo meu LDAP");   
    // We need to chase referrals when retrieving attributes from the DC   
    // as the object may be in a different domain   
     envDC.put(Context.REFERRAL, "follow");   
     // configura os atributos que serão obtidos na forma binária   
    envDC.put("java.naming.ldap.attributes.binary","objectGUID objectSid msSFUPassword");   
      // libera o LdapContext desta instancia, caso ele já estive atribuído   
    LdapContext ldapContext = new InitialLdapContext(envDC, null);   
     // Now perform a search against the GC   
    // Create the search controls   
    SearchControls searchCtls = new SearchControls();   
    // Specify the attributes to return   
    String[] returnedAtts = { "givenName", "objectGUID", "displayName",
	  "distinguishedName", "userPrincipalName", "samAccountName",   
      "objectClass", "sn", "unicodePwd", "userPassword",   
      "msSFUPassword" };   
     searchCtls.setReturningAttributes(returnedAtts);   
   // Specify the search scope   
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);   
   String searchFilter = "(&(objectClass=user)(userPrincipalName=" + (loginPesquisado) + "))";   
   // Search for objects in the GC using the filter   
   NamingEnumeration answer = ldapContext.search(searchBase, searchFilter,   
      searchCtls);   
System.out.println("entra no elementos");
if (answer.hasMoreElements()) {

SearchResult sr = (SearchResult) answer.next();

// Print out some of the attributes, catch the exception if the

// attributes have no values

Attributes attrs = sr.getAttributes();

// exibe os valores dos atributos retornados junto com seus valores

NamingEnumeration enumeration = attrs.getIDs();

while (enumeration.hasMoreElements()) {

String attrId = (String) enumeration.nextElement();

System.out.println(attrs.get(attrId));

}

System.out.println();
// obtem o displayName e nocaso dele ser null, pega o givenName   
  String userName = "";   
  if (attrs.get("displayName") != null) {   
      userName = (String) attrs.get("displayName").get();   
  } else if (attrs.get("givenName") != null) {   
      userName = (String) attrs.get("givenName").get();   
  }   
  // obtem o distinguishedName e o userPrincipalName   
  String distinguishedName = (String) attrs.get("distinguishedName")   
          .get();   
  String userPrincipalName = (String) attrs.get("userPrincipalName")   
          .get();   

  System.out.println(userName);   
  System.out.println(distinguishedName);   
  System.out.println(userPrincipalName);

}
}
}

Agraço a quem puder me ajudar!!!

1 Resposta

F

Certa vez concertei lendo esse post do forúm da SUN
http://forums.sun.com/thread.jspa?threadID=693373

Use as tags [code] para postar código fonte

Criado 5 de dezembro de 2008
Ultima resposta 5 de dez. de 2008
Respostas 1
Participantes 2