我有Active Directory中,有用戶,我試圖從Java程序更改用戶密碼如下:改變從java程序Active Directory用戶密碼
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
prop.put(Context.SECURITY_AUTHENTICATION, "simple");
prop.put(Context.SECURITY_PRINCIPAL,"user1");
prop.put(Context.SECURITY_CREDENTIALS,"pass1");
prop.put(Context.SECURITY_PROTOCOL,"ADSecurityProtocol");
prop.put(Context.PROVIDER_URL, "ldap://host:389/OU=My Org,DC=domain,DC=com");
try
{
LdapContext ctx =new InitialLdapContext(prop,null);
String oldPassword="pass1";
String newPassword="passnew1";
ModificationItem[] mods = new ModificationItem[2];
String oldQuotedPassword = "\"" + oldPassword + "\"";
byte[] oldUnicodePassword = oldQuotedPassword.getBytes("UTF-16LE");
String newQuotedPassword = "\"" + newPassword + "\"";
byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
new BasicAttribute("unicodePwd", oldUnicodePassword));
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
new BasicAttribute("unicodePwd", newUnicodePassword));
String theUserName="CN="+"user1"+",OU=My Org,DC=domain,DC=com";
// Perform the update
ctx.modifyAttributes(theUserName, mods);
System.out.println("Changed Password for successfully");
ctx.close();
}
catch (Exception e) {
System.err.println("Problem changing password: " + e);
}
錯誤消息我得到的是:
Problem changing password: javax.naming.NamingException:
[LDAP: error code 1 - 000020D6: SvcErr: DSID-031007DB,
problem 5012 (DIR_ERROR), data 0]; remaining name
'CN=user1,OU=My Org,DC=domain,DC=com'
編輯1:
根據建議,我已經與端口636和LDAPS作爲嘗試這種好:
prop.put(Context.PROVIDER_URL, "ldap://host:636/OU=My Org,DC=domain,DC=com");
Also tried
prop.put(Context.PROVIDER_URL, "ldaps://host:636/OU=My Org,DC=domain,DC=com");
I am getting MalformedURLException: Invalid URI:
Invalid URI: Org,DC=domain,DC=com]
當我嘗試(不知道什麼是對636聽,看來它是壽):
$ telnet LDAPHost 636
Escape character is '^]'.
Connection closed by foreign host.
EDIT2:
Changed:
prop.put(Context.PROVIDER_URL, "ldap://host:636/OU=My Org,DC=domain,DC=com");
to:
prop.put(Context.PROVIDER_URL, "ldap://host:636/OU=My%20Org,DC=domain,DC=com");
錯誤是:
javax.naming.CommunicationException: simple bind failed: host:636
[Root exception is java.net.SocketException: Connection reset]
也許LDAP服務器沒有監聽SSL端口:636
你已經解決了這個問題? – 2016-02-23 08:27:58