2009-07-02 61 views
3

我正尝试使用JNDI将条目添加到LDAP服务器。我可以成功读取来自LDAP服务器的条目。但是,当我尝试添加新条目时,我收到错误。我检查了各种方法,但我失败了。使用JNDI添加LDAP条目

private String getUserAttribs (String searchAttribValue) throws NamingException{ 
    SearchControls ctls = new SearchControls(); 
    ctls.setSearchScope(SearchControls.OBJECT_SCOPE); 

    Attributes matchAttrs = new BasicAttributes(true); 
    matchAttrs.put(new BasicAttribute("uid", searchAttribValue)); 
    NamingEnumeration answer = ctx.search("ou=People,ou=ABCLdapRealm,dc=abcdomain",matchAttrs); 

    SearchResult item =(SearchResult) answer.next(); 
    // uid userpassword description objectclass wlsmemberof sn cn 
    return item.toString(); 
} 

这工作正常。

然后我向前走了一步,试图添加一个条目。代码如下。

public static void bindEntry(DirContext dirContext)throws Exception{ 
    Attributes matchAttrs = new BasicAttributes(true); 
    // uid userpassword description objectclass wlsmemberof sn cn 
    matchAttrs.put(new BasicAttribute("uid", "defaultuser")); 
    matchAttrs.put(new BasicAttribute("userpassword", "password")); 
    matchAttrs.put(new BasicAttribute("description", "defaultuser")); 
    matchAttrs.put(new BasicAttribute("cn", "defaultuser")); 
    matchAttrs.put(new BasicAttribute("sn", "defaultuser")); 

    matchAttrs.put(new BasicAttribute("objectclass", "top")); 
    matchAttrs.put(new BasicAttribute("objectclass", "person")); 
    matchAttrs.put(new BasicAttribute("objectclass", "organizationalPerson")); 
    matchAttrs.put(new BasicAttribute("objectclass","inetorgperson")); 
    matchAttrs.put(new BasicAttribute("objectclass", "wlsUser")); 
    String name="uid=defaultuser"; 
    InitialDirContext iniDirContext = (InitialDirContext)dirContext; 
    iniDirContext.bind(name,dirContext,matchAttrs); 
} 

但是,我得到一个异常。

Exception in thread "main" javax.naming.OperationNotSupportedException: [LDAP: error code 53 - Unwilling To Perform]; remaining name 'uid=defaultuser' 

当然,我违反了一些东西。对此有何想法?

回答

4

LDAP 53,不愿意执行,通常意味着它说什么。您试图从LDAP服务器的角度来做一些“非法”的事情。

首先猜测,虽然不太可能,但您是否指向eDirectory?如果是这样,添加sn非常重要,因为它在eDirectory的模式中必须在创建时提供Surname值。在这种情况下,您可能会得到一个稍微不同的错误,更像是608或611错误。

第二次猜测,您指向的是Active Directory,在这种情况下,fullName是强制属性。但在这种情况下,你通常也会得到一些不同的结果代码。应该有更多的错误。 (尽管这可能是JNDI的回报,也是我使用的工具)。

再次猜测,您指向的人是LDAP服务器,而您错过了架构中的强制属性。

事实上,也许这是一个对象类的问题。 wlsUser是辅助类还是真正的类?在你的目录中,inetorgperson是一个真实的(我对这种类型的名称是空白的,还有辅助,结构和其他类)吗?

我的基本猜测是你错过了一个强制性属性,并且违反了目标目录中的模式,我希望上面列出的缺少强制性的可能示例是有帮助的。

+0

嗨geoffc,我也想我违反了原来的架构..你有一个工具的任何想法我可以转储模式或进行验证? – 2009-07-07 04:48:52

+0

你能得到cn = schema节点吗? Schema认为每个系统都是这样。寻找你的对象类,找出它继承的东西(SUP我认为),然后看看必须的属性。 (必须)。 – geoffc 2009-07-07 11:26:34

2

这是您尝试通过非SSL连接在Active Directory中设置密码时得到的错误。不用密码行再次尝试您的代码。

2

嗨通过使用下面的代码,我能够从JNDI程序中插入一个人变成LDAP

Attributes attributes=new BasicAttributes(); 
Attribute objectClass=new BasicAttribute("objectClass"); 
objectClass.add("inetOrgPerson"); 
attributes.put(objectClass); 

Attribute sn=new BasicAttribute("sn"); 
Attribute cn=new BasicAttribute("cn"); 

sn.add("sahul"); 
cn.add("vetcha"); 

attributes.put(sn); 
attributes.put(cn); 
attributes.put("title","software engg") 
ctx.createSubcontext("uid=sahul,ou=some organization7,o=some company7,ou=system",attributes);