LDAP java错误:添加entry.javax.naming.directory.NoSuchAttributeException

LDAP java错误:添加entry.javax.naming.directory.NoSuchAttributeException,java,ldap,jndi,Java,Ldap,Jndi,我在LDAP中添加条目时遇到问题,确切地说,我想添加一个用户 class AddUser { public static void main(String[] args) { String userName = "manager"; String password = "pass"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACT

我在LDAP中添加条目时遇到问题,确切地说,我想添加一个用户

class AddUser {

    public static void main(String[] args) {

        String userName = "manager";
        String password = "pass";
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://192.168.10.45:389/dc=mydc,dc=local");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, new String("mydc" + "\\" + userName));
        env.put(Context.SECURITY_CREDENTIALS, password);
        // env.put(Context.REFERRAL, "follow");
        // entry's DN
        String entryDN = "cn=NewUser, dc=mydc, dc=local";
        // entry's attributes
        Attribute cn = new BasicAttribute("cn", "NewUser");
        Attribute sn = new BasicAttribute("sn", "Smith");
        Attribute mail = new BasicAttribute("mail", "newuser@foo.com");
        Attribute phone = new BasicAttribute("telephoneNumber", "+1 222 3334444");
        Attribute uid = new BasicAttribute("uid", "nsmith");
        Attribute userPassword = new BasicAttribute("userPassword", "pwd1");
        Attribute oc = new BasicAttribute("objectClass");
        oc.add("dcObject");
        oc.add("person");
        oc.add("inetOrgPerson");

        DirContext ctx = null;

        try {
            // get a handle to an Initial DirContext
            ctx = new InitialDirContext(env);

            // build the entry
            Attributes entry = new BasicAttributes();
            entry.put(cn);
            entry.put(sn);
            entry.put(mail);
            entry.put(phone);
            entry.put(uid);
            entry.put(userPassword);
            entry.put(oc);

            // Add the entry
            ctx.createSubcontext(entryDN, entry);
            System.out.println("AddUser: added entry " + entryDN + ".");

        } catch (NamingException e) {
            System.err.println("AddUser: error adding entry." + e);
        }
    }
}
我得到了以下错误:

添加用户:添加时出错
entry.javax.naming.directory.NoSuchAttributeException:[LDAP:错误代码16-00000057:LDAPPER:DSID-0C090C3E,注释:属性转换操作错误,数据0,v1db1];剩余名称cn=NewUser,dc=mydc,dc=local


我不知道哪里出错了。只能是有关属性的错误输入?

正确的代码如下所示:

public static void main(String[] args) {

    String userName = "admin";
    String password = "s3cret";
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://192.168.10.10:389/DC=SOFTWAREDEV,DC=LOCAL");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, new String("softwaredev" + "\\" + userName));
    env.put(Context.SECURITY_CREDENTIALS, password);
    String path = "OU=SoftwareV3,OU=SOFTWARE";
    String newUser = "myUser"; // insert user here
    String entryDN = "CN=" + newUser + "," + path;
    Attribute cn = new BasicAttribute("cn", newUser);
    Attribute oc = new BasicAttribute("objectClass");
    oc.add("top");
    oc.add("person");
    oc.add("organizationalPerson");
    oc.add("user");

    DirContext ctx = null;

    try {
        ctx = new InitialDirContext(env);
        Attributes entry = new BasicAttributes();
        entry.put(cn);
        entry.put(oc);
        ctx.createSubcontext(entryDN, entry);
        System.out.println("AddUser: added entry " + entryDN + ".");

    } catch (NamingException e) {
        System.err.println("AddUser: error adding entry." + e);
    }
}

您正在使用
dcObject
objectclass。此OC要求对象上存在
dc
属性,这对人员来说没有多大意义(您不应该使用该objectclass)。我很确定这些信息被隐藏在错误的某个地方。。。您应该使用适当的日志机制(即带有Logback的SLF4J),并且不要抛出异常堆栈跟踪。好吧,删除该对象我得到了以下结果:[LDAP:错误代码32-0000208D:NameErr:DSID-0310020A,问题2001(没有对象),数据0,最佳匹配:'DC=MYDC,DC=LOCAL'这似乎是您的根上下文有问题…您确定它定义正确吗?换句话说,DN是错误的?发布更正的代码没有多大意义,除非您告诉我们它是如何更正的。是的。我使用wireshark和ldapadmin查看到r的正确数据包头正确地使用它。