2011-08-31 80 views
0

正试图从ADAM Active Directory获取用户详细信息。 我使用了下面的java代码。ldap命名异常

import javax.naming.*; 
import javax.naming.directory.*; 
import java.util.Hashtable; 

public class SimpleQuery { 
public static void main(String[] args) { 

      StringBuffer output = new StringBuffer(); 
    attribute="street"; 
    query="(&(cn=ldap1))"; 

    try { 
     String url = "ldap://172.16.12.178:389/cn=users,dc=sharepoint,dc=com"; 
     Hashtable env = new Hashtable(); 
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.PROVIDER_URL, url); 
     DirContext context = new InitialDirContext(env); 

     String[] attributeFilter = {"cn", "givenName", "SN", "street", "streetAddress", "postalAddress", "postOfficeBox", "l", "st", "postalCode", "c", "telephoneNumber", "homePhone", "mobile", "pager", "mail", "objectCategory", "objectClass", "userAccountControl"}; 
     SearchControls ctrl = new SearchControls(); 
     ctrl.setReturningAttributes(attributeFilter); 
     ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE); 
     NamingEnumeration enumeration = context.search("", query, ctrl); 
     while (enumeration.hasMore()) { 
      SearchResult result = (SearchResult) enumeration.next(); 
      Attributes attribs = result.getAttributes(); 
      NamingEnumeration values = ((BasicAttribute) attribs.get(attribute)).getAll(); 
      while (values.hasMore()) { 
       if (output.length() > 0) { 
       output.append("|"); 
       } 
       output.append(values.next().toString()); 
      } 
     } 

    } catch (Exception e) { 
     System.out.println("Exception : "+e); 
     e.printStackTrace(); 
    } 
    System.out.print(output.toString()); 
} 

public SimpleQuery() {}} 

,并正在此异常:

javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C09 
06DC, comment: In order to perform this operation a successful bind must be comp 
leted on the connection., data 0, v1db0 ]; remaining name '' 

有我需要把任何属性?

请指导我。

谢谢

回答

3

您正在匿名尝试操作,而且这是不允许的。在尝试操作之前,您需要在env中设置Context.SECURITY_PRINCIPAL/SECURITY_CREDENTIALS。

+0

谢谢,你是对的。我使用Principal&Credentials属性得到了结果。 – Mohan