简体   繁体   English

Java-LDAP:属性为只读

[英]Java - LDAP: Attribute is Read-Only

I am using UnboundID-LDAPSDK (2.3.8) to change the user's photo in our Microsoft Active Directory. 我正在使用UnboundID-LDAPSDK(2.3.8)在我们的Microsoft Active Directory中更改用户的照片。

LDAPConnection ldap = null;
        try {
            ldap = new LDAPConnection("domain-srv", 389, "CN=admin,OU=Users,OU=ADM,DC=domain,DC=local", "password");
            SearchResult sr = ldap.search("DC=domain,DC=local", SearchScope.SUB, "(sAMAccountName=" + getUser().getUsername() + ")");
            if (sr.getEntryCount() == 1) {
                SearchResultEntry entry = sr.getSearchEntries().get(0);
                entry.setAttribute("thumbnailPhoto", getUser().getPhotoAsByteArray());

                ldap.close();
                return true;
            } else
                return false;

        } catch (LDAPException e) {
            e.printStackTrace();
        }

But I get a java.lang.UnsupportedOperationException. 但是我得到了一个java.lang.UnsupportedOperationException。

The documentation for setAttribute states: setAttribute的文档指出:

Throws an UnsupportedOperationException to indicate that this is a read-only entry. 引发UnsupportedOperationException表示这是一个只读条目。

I also tried to change the postalCode but I get the same exception. 我也尝试更改了postalCode,但遇到了同样的异常。

Changing those attributes should be possible, because I can change them with jXplorer. 更改那些属性应该是可能的,因为我可以使用jXplorer进行更改。

Do I have to enable a write-mode somehow? 我是否必须以某种方式启用写模式?

Thank you 谢谢

The SearchResultEntry object extends ReadOnlyEntry and is therefore immutable. SearchResultEntry对象扩展了ReadOnlyEntry,因此是不可变的。 But even if it weren't, merely calling entry.setAttribute would have no effect on the data in the server. 但是,即使不是,仅调用entry.setAttribute也不会对服务器中的数据产生影响。 You have to use a modify operation for that. 您必须为此使用修改操作。

To do that, you'd need something like: 为此,您需要类似以下内容:

 ModifyRequest modifyRequest = new ModifyRequest(entry.getDN(),
      new Modification(ModificationType.REPLACE,
           "thumbnailPhoto", getUser().getPhotoAsByteArray());
 ldap.modify(modifyRequest);

Also, you should put the call to ldap.close() in a finally block because as the code is written now, you're only closing the connection if the search is successful and returns exactly one entry, but not if the search fails, doesn't match any entries, or the attempt to perform the modify fails. 另外,您应该将对ldap.close()的调用放在finally块中,因为在编写代码时,只有在搜索成功并返回一个正确的条目的情况下才关闭连接,而在搜索失败的情况下则不会,与任何条目都不匹配,或者尝试执行修改失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM