Java-LDAP : Attribute is Read-Only

标签 java active-directory ldap unboundid-ldap-sdk

我正在使用 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();
        }

但是我得到了 java.lang.UnsupportedOperationException。

setAttribute 的文档指出:

Throws an UnsupportedOperationException to indicate that this is a read-only entry.

我也尝试更改邮政编码,但遇到了相同的异常。

更改这些属性应该是可能的,因为我可以使用 jXplorer 更改它们。

我必须以某种方式启用写入模式吗?

谢谢

最佳答案

SearchResultEntry 对象扩展了 ReadOnlyEntry,因此是不可变的。但即使不是,仅仅调用entry.setAttribute也不会对服务器中的数据产生影响。您必须为此使用修改操作。

为此,您需要类似以下内容:

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

此外,您应该将对 ldap.close() 的调用放在 finally block 中,因为按照现在编写的代码,只有在搜索成功并仅返回一个条目时才关闭连接,但如果搜索失败、不匹配任何条目或尝试执行修改失败。

关于Java-LDAP : Attribute is Read-Only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28881859/

相关文章:

java - ListView 仅显示 ListView 第一个位置的最后一项

c# - .NET 4.5 UserPrincipal.FindByIdentity (System.DirectoryServices.AccountManagement) 中的错误

asp.net-mvc - 更改 Active Directory 安全组和用户角色的时间滞后

Java LDAP 始终返回单个值而不是列表

java - 如何在 tomcat 上使用 LDAP 设置 CAS .. 没有错误

Java:使用 RxTx 库

java - 为什么我的证书没有在 https 上的 jboss 4.2 上启用?

c++ - 从 C/C++ 向 LDAP 服务器进行身份验证

java - 从 JFrame 获取输入

python - Pyramid (Pylons/repoze.bfg) LDAP 身份验证