2009-07-02 59 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);