javax.naming.directory.SchemaViolationException:[LDAP:错误代码 65 - 对象 Class 违规];

javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - Object Class Violation];

在此代码中,我无法将一个用户添加到组中。这里的 uid 是用户。这里 cn=citizens,cn=doit,o=evault 是组完整 DN 并且我尝试使用 memberOf 而不是成员。但它仍然显示相同的异常。

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, initctx);
        env.put(Context.PROVIDER_URL, myhost);
        env.put(Context.SECURITY_PRINCIPAL, mgrdn);
        env.put(Context.SECURITY_CREDENTIALS, mgrpw);
        System.out.println("Connect");
        String entryDN = "uid=datta,cn=doit,o=evault";

        // entry's attributes
        BasicAttribute cn = new BasicAttribute("cn", "datta");
        BasicAttribute sn = new BasicAttribute("sn", "kumar");
        BasicAttribute mail = new BasicAttribute("mail", "sai@xx.com");
        BasicAttribute phone = new BasicAttribute("telephoneNumber", "9704763492");
        BasicAttribute uid = new BasicAttribute("uid", "datta");
        BasicAttribute member = new BasicAttribute("member", "cn=citizens,cn=doit,o=evault");

        BasicAttribute oc = new BasicAttribute("objectClass");
        oc.add("top");
        //oc.add("person");
        oc.add("groupOfNames");

        ((javax.naming.directory.Attribute) oc).add("organization");
       // ((javax.naming.directory.Attribute) oc).add("inetOrgPerson");
        ((javax.naming.directory.Attribute) oc).add("groupOfNames");

        DirContext ctx = new InitialDirContext(env);

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


        // Add the entry
        ctx.createSubcontext(entryDN, (javax.naming.directory.Attributes) entry);

此处显示错误为:

javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - Object Class Violation]; remaining name 'uid=datta,cn=doit,o=evault'

您似乎完全不清楚是要添加用户、组、组织还是用户到组。

直接的问题是 groupOfNames 没有扩展 organization,或者 反之亦然, 它们都是结构对象 类 ,因此您不能在同一个对象中同时指定它们。正如消息所说,这是架构违规。

注意您不需要将 BasicAttribute 转换为 Attribute.