2016-08-23 30 views
1

我們正在開發一個自定義密碼重置工具,它目前能夠爲用戶重置密碼(使用管理員DN),但我還需要刪除/修改某些操作屬性才能完全處理業務用例。我連接到LDAP服務器使用:如何從Java/JNDI修改OpenLDAP中的操作屬性?

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; 
} 

,我需要修改一些操作屬性做的事情像解鎖賬戶/更新修改時間的/ etc ...

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 

誰能幫我弄清楚爲什麼?

+0

'modifyTimtestamp'屬性不可由用戶修改。奇怪的是,更新密碼或者什麼都不會更新這個屬性。也許只有修改其他屬性才能實現這一點。 –

回答

1

如何從Java/JNDI修改OpenLDAP中的操作屬性?

你不知道。服務器呢。這就是「操作屬性」的意思。

我還需要刪除/修改某些操作屬性,以完整地處理業務用例

運氣不好。

你應該使用'ppolicy'覆蓋和相關的擴展密碼修改操作,而不是自己滾動所有這些。它可以滿足您的一切需求,如果不需要調整您的需求;-)

注意您不應該自己哈希密碼。如果配置正確,OpenLDAP將爲您做到這一點。

+0

ppolicy IS就位並且在普通用戶更新密碼時效果很好。問題是這是一個密碼重置工具,它意味着它作爲管理員DN進行身份驗證,並且ppolicy不會阻止管理員DN執行任何操作。 –

+1

不,問題是(a)您沒有在上面的代碼中使用擴展密碼修改操作,這意味着您完全繞過了使用此代碼的ppolicy覆蓋層,並且(b)您不應該使用內部管理器DN適用於任何情況:您應該定義具有適當權限的管理用戶並使用它。經理DN是OoenLDAP本身。它不應該被任何人或應用程序使用。 – EJP

+0

您能否進一步解釋或提供鏈接? –