[英]Change password in LDAP using sAMAccountName
I have the following code that changes an user password in LDAP. 我有以下代码可以更改LDAP中的用户密码。 It works if I use the user's CN, but I have no idea of what should I do to do it work with the sAMA.
如果我使用用户的CN,它可以工作,但是我不知道应该如何使用sAMA。
public static void main(String[] args) {
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, "user_conect@ifjac.redelocal");
prop.put(Context.SECURITY_CREDENTIALS, "ifpr2018");
prop.put(Context.SECURITY_PROTOCOL, "ADSecurityProtocol");
prop.put(Context.PROVIDER_URL, "ldap://localhost/OU=Group,DC=ifjac,DC=redelocal");
try {
LdapContext ctx = new InitialLdapContext(prop, null);
String oldPassword = "old";
String newPassword = "new";
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=user, OU=Users";
ctx.modifyAttributes(theUserName, mods);
System.out.println("Changed Password for successfully");
ctx.close();
} catch (Exception e) {
System.err.println("Problem changing password: " + e);
}
}
You need the find the user in AD using the sAMAccountName
first and take the DN out of the search result and put it into the theUserName
variable. 您需要首先使用
sAMAccountName
在AD中找到用户,然后将DN从搜索结果中theUserName
并将其放入theUserName
变量中。
Create a filter that looks like this: 创建一个看起来像这样的过滤器:
(sAMAccountName={theSamAccountName})
Then use it to search the directory. 然后使用它搜索目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.