ldap命名异常

时间:2011-08-31 09:35:04

标签: ldap

我正在尝试从ADAM Active Directory获取用户详细信息。 我使用了以下java代码。

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 ''

我需要放置任何属性吗?。

请指导我。

由于

1 个答案:

答案 0 :(得分:3)

您正在匿名尝试此操作,但不允许这样做。在尝试操作之前,您需要在env中设置Context.SECURITY_PRINCIPAL / SECURITY_CREDENTIALS。