0

我有一个 LDAP 属性,它存储多个值,即用户详细信息(如名字、姓氏、电子邮件地址)存储在一个带有键值对的属性中。

例如,属性名称='Testuser'。这个 'Testuser' 属性如下多个值:firstname=test, lastname=testing email=testing@xxx.com 像这样。

现在我想使用 java 代码单独修改名字值。(我搜索了许多网站,我可以使用 ModificationItem 找到单个属性更改)

这是我的代码片段:

DirContext ctx = new InitialDirContext(env);
                  SearchControls ctls = new SearchControls();
                ctls.setReturningObjFlag(true);
                ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                                String filter;
                filter = "(&(objectClass=myobjectclass)(apn=" + userName + "))";

                                 NamingEnumeration answer = ctx.search("o=mydomain", filter, ctls);



while (answer.hasMore()) {
    SearchResult sr = (SearchResult)answer.next();
        Attributes attrs = sr.getAttributes();
        String givenName = " ";
try {
                for (NamingEnumeration e = attrs.getAll(); e.hasMore();) {

                    Attribute attr = (Attribute) e.next();

                    System.out.println("Attribute name: " + attr.getID());



                    for (NamingEnumeration n = attr.getAll(); n.hasMore(); System.out

                            .println("value: " + n.next()));
}} catch (Exception err) {
givenName = " ";
}
  }

我得到以下输出:

Attribute name: apn
value: testuser
Attribute name: appropertycollection
value: Profile.Contact.ZipCode=46784157
value: Profile.Contact.State=7
value: Profile.Contact.MobileNum=4564545455
value: Profile.Contact.Password=12345
value: Profile.Contact.FirstName=David
value: Profile.Contact.Address=TestAddress456
value: Profile.Contact.Email=asde@xxx.com
value: Profile.Contact.LastName=lastname

现在我想单独修改属性集合属性中的“Profile.Contact.FirstName=David”值。

非常感谢您的帮助。

谢谢!

4

1 回答 1

1

创建一个javax.naming.directory.BasicAttribute怎么样,添加所有必要的属性。

然后,按照此处的简短教程进行操作:Oracle 教程

请向我提供一个简短的代码片段,以便我们看到您为之苦恼的事情。

于 2013-08-28T16:20:42.270 回答