如何从Java / JNDI修改OpenLDAP中的操作属性?

时间:2016-08-23 16:22:59

标签: java ldap jndi openldap

我们正在开发一个自定义密码重置工具,该工具目前能够重置用户的密码(使用管理员DN),但我还需要删除/修改一些操作属性,以便完全处理业务用例。我使用:

连接到LDAP服务器
private void connect() throws NamingException {
    Properties props = new Properties();
    props.put(INITIAL_CONTEXT_FACTORY, LDAP_CTX_FACTORY);
    props.put(PROVIDER_URL, format("ldap://%s:%d/", config.ldapHost(), config.ldapPort()));
    props.put(SECURITY_CREDENTIALS, config.ldapBindPassword());
    props.put(SECURITY_PRINCIPAL, config.ldapBindUser());
    props.put(SECURITY_AUTHENTICATION, "simple");
    props.put(REFERRAL, "follow");
    props.put(BATCHSIZE, "1000");
    connection = new InitialLdapContext(props, null);
    connection.setRequestControls(LDAPControls.controls());

    LOG.debug("Successfully completed bind to LDAP server '{}'", config.ldapHost());
    connected = true;
}

我需要修改一些操作属性来执行诸如解锁帐户/更新修改时间等等...

    List<BasicAttribute> attrs = new ArrayList<>();
    List<ModificationItem> mods = new ArrayList<>();
    // Set password hash
    attrs.add(new BasicAttribute("userPassword", "{SSHA}" + hashPassword(salt, password)));
    mods.add(new ModificationItem(REPLACE_ATTRIBUTE, attrs.get(0)));
    // Set last modified timestamp
    attrs.add(new BasicAttribute("modifyTimestamp", date.withZone(UTC).format(now())));
    mods.add(new ModificationItem(REPLACE_ATTRIBUTE, attrs.get(1)));
    // Set password changed time
    attrs.add(new BasicAttribute("pwdChangeTime", date.withZone(UTC).format(now())));
    mods.add(new ModificationItem(REPLACE_ATTRIBUTE, attrs.get(2)));
    // Remove password lock
    attrs.add(new BasicAttribute("pwdAccountLockedTime"));
    mods.add(new ModificationItem(REMOVE_ATTRIBUTE, attrs.get(3)));
    // Clear password failure time
    attrs.add(new BasicAttribute("pwdFailureTime"));
    mods.add(new ModificationItem(REMOVE_ATTRIBUTE, attrs.get(4)));
    this.reconnect();
    ModificationItem[] modItems = new ModificationItem[mods.size()];
    mods.toArray(modItems);
    connection.modifyAttributes(getDN(email), modItems);
    LOG.debug("Completed update of user password for '{}'", email);
    return true;

但是当我跑步时,我得到:

LDAP: error code 21 - modifyTimestamp: value #0 invalid per syntax

有人能帮我弄清楚原因吗?

1 个答案:

答案 0 :(得分:1)

  

如何从Java / JNDI修改OpenLDAP中的操作属性?

你没有。服务器呢。这就是“操作属性”的含义。

  

我还需要删除/修改一些操作属性,以便完全处理业务用例

运气不好。

你应该使用'ppolicy'覆盖和相关的扩展密码修改操作,而不是自己滚动所有这些。它可以满足您的一切需求,如果不需要,您需要调整您的需求; - )

注意:您不应自行散列密码。正确配置后,OpenLDAP将为您执行此操作。