java - 从 java 程序更改 Active Directory 用户密码

标签 java active-directory passwords ldap

我有 Active Directory,其中有用户,我正在尝试从 Java 程序更改用户密码,如下所示:

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.

编辑2:

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

最佳答案

[The unicodePwd] attribute can be written under restricted conditions [...] In order to modify this attribute, the client must have a 128-bit Secure Socket Layer (SSL) connection to the server.

您只有一个普通的不安全 ldap:// 连接而不是 ldaps://,因此根据上述限制,这将不起作用。

查看更多详情: http://support.microsoft.com/kb/269190

关于java - 从 java 程序更改 Active Directory 用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15335614/

相关文章:

java - 为什么我的curl命令不能与Java ProcessBuilder API一起使用

java - isEnabled() 方法始终返回 true

apache - 如何防止Internet Explorer在Domain匹配AD-Domain时以Intranet模式显示页面

c# - 以编程方式设置 Active Directory 密码时,如何要求更改密码?

javascript - 不同语言的最常用密码

java - 字符串的有序固定长度组合

linux - 如何使用多个过滤器进行 ldapsearch?

c++ - 在 AD 服务器上使用 winldap.h 进行 LDAP 搜索

passwords - 用户的密码是否应该限制为特定的字符集或强度?

java - 向类添加字段导致 "<constructor> does not define an index 2"异常