如何在验证一个用户后更改其他用户密码

时间:2011-12-13 15:01:31

标签: java ldap spring-ldap

我已经成功验证了一个用户。现在我想更改其他用户的密码。我的第一个用户就像管理员一样,所以其他用户的密码应该被更改或重置。

我正在使用以下代码:

LdapContext ctx = null;     
Hashtable env = new Hashtable();            
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "password");
env.put(Context.PROVIDER_URL, "ldap://xx.xxx.xx.xxx:389");

ctx = new InitialLdapContext(env, null);     

final DistinguishedName dn = usernameMapper.buildDn(username);

final ModificationItem[] passwordChange = new ModificationItem[] 
{
    new ModificationItem(DirContext.REPLACE_ATTRIBUTE, 
                         new BasicAttribute(passwordAttributeName, newPassword))
};

ctx.modifyAttributes(dn, passwordChange);

System.out.println("Password changed successfully");
ctx.close();

字段属性值在这里:

 String passwordAttributeName = "userPassword";
 static LdapUsernameToDnMapper usernameMapper 
                      = new DefaultLdapUsernameToDnMapper("OU=DROID-TEST,DC=example,DC=com",
                                                          "cn");
 private String username = "test01";
 private String password = "test01";
 private String newPassword = "123";

我正在关注异常:

javax.naming.NoPermissionException: [LDAP: error code 50 - 00002098: SecErr: DSID-03150A48, problem 4003 (INSUFF_ACCESS_RIGHTS), data 0    

1 个答案:

答案 0 :(得分:2)

NoPermissionException表示您连接到LDAP的用户无权替换该属性。您需要为用户提供更高级别访问权限。

此外,根据LDAP提供程序,您可能需要通过安全的ldaps连接而不是基本的ldap连接进行连接。

相关问题