如何使用JNDI将OU添加到LDAP

时间:2010-08-10 09:21:49

标签: java ldap jndi

我想从JNDI向LDAP添加新的OU。我的LDAP服务器是从OpenDS设置的。

这是我的代码:

public static void main(String args[])
{
    String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
    String MY_HOST = "ldap://localhost:1389";
    String MGR_DN = "cn=Directory Manager";
    String MGR_PW = "password";
    String MY_SEARCHBASE = "dc=QuizPortal";

    try
    {
        Hashtable env = new Hashtable();

        env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

        env.put(Context.PROVIDER_URL, MY_HOST);

        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
        env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

        DirContext ctx = new InitialDirContext(env);

        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);

        ctx.createSubcontext("ou=NewOu", attrs);
    }

    catch(Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }
}

以下是错误消息:

javax.naming.NameNotFoundException: [LDAP: error code 32 - The provided entry ou=NewOu cannot be added because it does not have a parent and is not defined as one of the suffixes within the Directory Server]; remaining name 'ou=NewOu'
        at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3066)
        at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2987)
        at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2794)
        at com.sun.jndi.ldap.LdapCtx.c_createSubcontext(LdapCtx.java:788)
        at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_createSubcontext(ComponentDirContext.java:319)
        at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:248)
        at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(PartialCompositeDirContext.java:236)
        at javax.naming.directory.InitialDirContext.createSubcontext(InitialDirContext.java:178)
        at JUNDIAdd2.main(JUNDIAdd2.java:43)

添加信息:我有o=IT, dc=QuizPortal我要添加到新OU中。

有人可以指导我解决这个错误吗?

2 个答案:

答案 0 :(得分:4)

尝试:

ctx.bind("ou=NewOu,o=IT", null, attrs);

您可能需要在之前设置ou属性:

attrs.put("ou", "NewOu");

答案 1 :(得分:1)

你试过了吗?

ctx.createSubcontext("ou=NewOu,dc=QuizPortal,o=IT", attrs);

或者这个:

ctx.createSubcontext("ou=NewOu,dc=QuizPortal", attrs);

希望这会对你有所帮助