密码过期后,无需管理员帐户JNDI LDAP AD即可更改密码

时间:2020-04-28 15:05:03

标签: java active-directory ldap jndi

我有以下问题。我尝试成功通过JNDI更改具有管理员帐户的AD用户的密码。问题是,现在我没有该管理员访问权限。

我要使用此密码更改功能的唯一一件事是在用户密码过期时更改它。

是否可以使用他自己的凭据(用户名和旧密码)更改用户密码(即使使用JNDI已过期)?如果可能的话,我应该在我的代码中做什么?

这是我当前的代码

public String changePassword(String username, String password, String newPassword)
{
    Hashtable env = new Hashtable();
    String domain = (String) context.getBean("domain");

    String base = (String) context.getBean("base");

    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");


    env.put(Context.SECURITY_AUTHENTICATION, "simple");

    env.put(Context.SECURITY_PRINCIPAL, username + domain);


    env.put(Context.SECURITY_CREDENTIALS, password );

    env.put(Context.PROVIDER_URL,context.getBean("url"));


    env.put(Context.SECURITY_PROTOCOL, "ssl");

    try {
        // Create the initial directory context
        LdapContext ctx = new InitialLdapContext(env,null);


        ModificationItem[] mods = new ModificationItem[2];


        String oldQuotedPassword = "\"" + password + "\"";
        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));


        ctx.modifyAttributes("cn=" + username + ',' + base, mods);

        ctx.close();

        return "Changed Password for: " + username;
    }
    catch (NamingException e) {
        return new String ("Problem changing password: " + e);
    }
    catch (UnsupportedEncodingException e) {
        return new String("Problem encoding password: " + e);
    }

}

如果没有,我也可以使用Spring LDAP。在那里,有可能吗?怎么样? 在此先感谢您,如果我不能很好地解释自己,对不起,这是我在这里的第一篇文章

0 个答案:

没有答案
相关问题