2012-07-04 31 views
5

我在查找如何將#Ldap用戶關聯到給定組時遇到問題。將Ldap用戶關聯到Java的組

這就是我曾嘗試:

Attributes attrs = new BasicAttributes(); 

    BasicAttribute basicAttrs = new BasicAttribute("objectclass"); 
    basicAttrs.add("top"); 
    basicAttrs.add("person"); 

    BasicAttribute memberOf = new BasicAttribute("memberOf"); 
    memberOf.add("Managers"); // Tried with distinguished name too 
    memberOf.add("Administrators"); // Tried with distinguished name too 

    attrs.put(basicAttrs); 
    attrs.put("cn", user.getLogin()); 
    attrs.put("name", user.getLogin()); 
    attrs.put("login", user.getLogin()); 
    attrs.put("mail", user.getMail()); 
    attrs.put("displayName", user.getDisplayName()); 
    attrs.put("memberOf", memberOf); 

    try { 
     ctx.bind("CN=" + user.getLogin() + "," + baseDn, null, attrs); 
    } catch (NamingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我也嘗試過使用的專有名稱,如:「CN =經理,OU = <系統名稱>,OU =用戶,OU = <服務器>,DC = com「,但沒有工作。 我認爲它應該在某處引用Ldap組。

但我得到這個錯誤:

javax.naming.directory.InvalidAttributeValueException: Malformed 'memberOf' attribute value; remaining name 'CN=lcarvalho,OU=<system_name>,OU=Users,OU=<server>,DC=com' 
at com.sun.jndi.ldap.LdapClient.encodeAttribute(LdapClient.java:951) 
at com.sun.jndi.ldap.LdapClient.add(LdapClient.java:999) 
at com.sun.jndi.ldap.LdapCtx.c_bind(LdapCtx.java:396) 
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_bind(ComponentDirContext.java:277) 
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.bind(PartialCompositeDirContext.java:197) 
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.bind(PartialCompositeDirContext.java:186) 
at javax.naming.directory.InitialDirContext.bind(InitialDirContext.java:158) 
... 

這是除了我的申請行的所有堆棧跟蹤。

+0

我還沒有使用JNDI來使用LDAP,但是如果'person'對象類具有'memberOf'屬性,您是否已經檢入了LDAP服務器?什麼是您正在開發的LDAP服務器?無論如何,你必須爲'memberOf'指定完整的DN。 – hovanessyan

+0

我現在只是爲了測試這個而嘗試了同樣的方法,可以重現問題。 'memberOf'屬性確實存在,並且DN是正確的。但我認爲這裏的主要問題是,將用戶添加到組中應該發生在組對象中,而不是用戶對象(因爲@EJP在下面進一步評論)。 –

回答

0

最有可能您的DN是錯誤的,因爲它似乎你指定一個額外的組織單位,而不是域組件:

"CN=Managers,OU=<system_name>,OU=Users,OU=<server>,DC=com" 

應該是:

"cn=Managers,ou=<system_name>,ou=Users,dc=<server>,dc=com" 

在LDAP目錄結構開始與2域組件,這是一個顛倒的公司域名(按慣例)。

爲了使您的代碼工作,你必須要考慮到以下幾點:

  • 有一個模式的「人」是在您的LDAP服務器加載

  • 有一個屬性「的memberOf人 「模式

  • 」成員「在你的定義」,「 需要完整DN作爲入門

我也鼓勵你看看UnboundID LDAP SDK

希望有所幫助。

0

memberOf屬性的值錯誤。 memberOf屬性可能是一個可分辨的名稱。如果對屬性的語法,排序或匹配規則有疑問,LDAP客戶端應查閱架構(基礎DN可能在root DSE中可用)。

+0

謝謝,但我也嘗試使用可分辨的名稱,我省略了baseDn,但這裏是: 'String baseDn =「OU = Delphos,OU = Users,OU = Nextel,DC = tades,DC = com,DC = BR「'; 這裏是我使用的兩個着名名稱: 'memberOf.add(「CN = Gestores,OU = Delphos,OU = Roles,OU = Nextel,DC = tades,DC = com,DC = br 「); 我有在Ldap服務器中創建的角色和用戶。我還創建了屬性「memberOf」,當我嘗試省略它時,錯誤更改爲「屬性轉換操作中的錯誤,數據0,v1db1」 –

3

如果您使用OpenLDAP,memberOf屬性由memberOf覆蓋層自動維護,並且您的應用程序不應該編寫它。你應該做的是將用戶的DN添加到他加入的組中的uniqueMember或roleOccupant等屬性。然後它的DN會神奇地出現在他的memberOf屬性中。

+1

此答案是正確的,應該標記爲。它也證實了這個問題的答案:http://stackoverflow.com/questions/21147625/java-ldap-add-group-to-user-issue-error-code-53-will-not-perform –

-1

我有同樣的問題。使用ldap的任何客戶端(例如:Apache Directory Studio)檢查此屬性的值類型。如果你試圖用int值替換String類型的屬性,它會拋出這個錯誤。