2012-03-14 84 views
4

我沒有(也不會)有管理員帳戶。 我想在Java中更改Active Directory中的自己(用戶)密碼。 我該怎麼做?我可以更改自己的Active Directory密碼從LDAP(沒有管理帳戶)

從網頁使用代碼:

private void changePass() throws Exception { 
    String oldpass = this.encodePassword("oldpass!"); 
    String newpass = this.encodePassword("newpass!"); 
    Attribute oldattr = new BasicAttribute("unicodePwd", oldpass); 
    Attribute newattr = new BasicAttribute("unicodePwd", newpass); 
    ModificationItem olditem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, oldattr); 
    ModificationItem newitem = new ModificationItem(DirContext.ADD_ATTRIBUTE, newattr); 
    ModificationItem repitem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, newattr); 
    ModificationItem[] mods = new ModificationItem[2]; 
    mods[0] = olditem; 
    mods[1] = newitem; 
    // ldapTemplate.modifyAttributes("cn=administrator,cn=Users", mods); 
    ldapTemplate.modifyAttributes("cn=smith,cn=Users", new ModificationItem[] { repitem }); 
} 

這裏是好的ContextSource

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> 
    <property name="url" value="ldap://ldapserver:389"/> 
    <property name="base" value="dc=company,dc=com"/> 
    <property name="userDn" value="[email protected]"/> 
    <property name="password" value="oldpass"/> 
</bean> 

我:

LDAP: error code 32 - 0000208D: NameErr: DSID-0310020A, problem 2001 (NO_OBJECT), data 0, best match of: 
'CN=Users,DC=company,DC=com' 

如果我改變用戶DN爲 「CN =史密斯」 我得到:

LdapErr:DSID-0C0903A9,註釋:AcceptSecurityContext錯誤

也許我的問題是,我不明白LDAP是如何工作的?是否可以(使用用戶帳戶更改用戶密碼)? 而且,如果可能的話,我可以使用相同的權限檢查帳戶鎖定/過期嗎?

UPDATE/RESOLVE

非常感謝你的比賽對你有所幫助。我也非常有幫助。

爲未來的搜索:

NO_OBJECT - 意味着Active Directory中找不到對象(我CN =用戶,CN = Smith)的 要查找用戶目錄,你可以使用用戶屬性「完全合格的標準路徑的distinguishedName 「(在我,worstest情況下,它是」 CN =約翰\,史密斯 「OU =承包商,OU =用戶帳戶,OU =帳戶」

後來我:

WILL_NOT_PERFORM - 這可能意味着不同類型的東西。在我的情況下,存在錯誤的對象類型,但是,可能出現其他情況,如下所述 - 不是SSL連接(而不是ldaps://)等。

則:

INSUFF_ACCESS_RIGHTS - 用戶(不管理員不有權更換密碼屬性),更改密碼,他必須輸入舊密碼和新密碼,然後刪除舊的和新增。

Attribute oldattr = new BasicAttribute("unicodePwd", oldQuotedPassword.getBytes("UTF-16LE")); 
Attribute newattr = new BasicAttribute("unicodePwd", newQuotedPassword.getBytes("UTF-16LE")); 
ModificationItem olditem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, oldattr); 
ModificationItem newitem = new ModificationItem(DirContext.ADD_ATTRIBUTE, newattr); 
ldapTemplate.modifyAttributes("cn=John\\, Smith,ou=Contractors,ou=User Accounts,ou=Accounts", new ModificationItem[] { olditem, newitem }); 

問題1005(CONSTRAINT_ATT_TYPE) - 如果舊密碼錯誤

BTW

javax.naming.PartialResultException:未處理繼續參考(S); 剩餘名稱'/' - 搜索人員/用戶全局(例如,在驗證方法中) ldapTemplate。setIgnorePartialResultException(true);可以修復它

回答

1
  1. 如果cn=smith,cn=Users不是進入真正的DN,它需要。

  2. 你不需要所有的刪除/添加/替換的東西:只需使用REPLACE_ATTRIBUTE; 如果您正在使用管理帳戶更改密碼。

    如果您以自己的身份更新密碼,即綁定到正在更新的同一帳戶時,您確實需要它。原因是您必須提供舊密碼才能刪除,新密碼需要插入,以便可以檢測到舊密碼的匹配失敗。或者,您可以使用擴展密碼修改操作,其中再次提供舊密碼和新密碼。

+0

非常感謝你,我固定的代碼的getContext()的modifyAttributes( 「CN =用戶」,新的ModificationItem [] {新的ModificationItem(DirContext.REPLACE_ATTRIBUTE,新的BasicAttribute(「unicodePwd 「,encodePassword(」mamakassa22!「)。getBytes(」UTF-16LE「)))});和cn = Users,現在我得到了SvcErr:DSID-03190C6B,問題5003(WILL_NOT_PERFORM),數據爲0.也許問題是不是SSL連接? – xtern 2012-03-14 11:26:17

+0

@JohnSmith我相信WILL_NOT_PERFORM意味着它需要比迄今爲止更多的安全性。您還應該考慮使用擴展密碼修改操作,而不僅僅是此屬性更改,如果您有API的話。 – EJP 2012-03-15 04:05:57

+0

如果您在更改密碼屬性時使用非管理員用戶進行身份驗證,則確實需要刪除/添加內容。在這種情況下沒有簡單的REPLACE_ATTRIBUTE。 – Johanneke 2015-03-02 16:27:51

4

是的,你可以,但它有點棘手。

首先要更改密碼,您必須通過LDAPS而不是LDAP進行連接。這是與TLS或SSL(至少128位)連接。這是一個例子,可以用JNDI來完成。

其次,您必須將密碼作爲UTF-16LE編碼的字節數組傳遞。但在編碼之前,你應該把它放在雙引號中。所以這裏有一個例子:

String pass = "\"" + "newpass" + "\""; 
byte[] password = pass.getBytes("UTF-16LE"); 
// You will need to handle UnsupportedEncodingException here 
相關問題