java - 通过 Spring LDAP 更改 Active Directory 密码

标签 java spring active-directory ldap

我有一个 Java 应用程序,我希望每个用户都可以通过应用程序更改自己的密码。

这是我的代码:

public void changePassword()
{
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl("LDAPS://X.Y.Z.T/");
    contextSource.setBase("DC=example,DC=com");
    contextSource.setUserDn("username@example.com");
    contextSource.setPassword("oldpass");
    contextSource.afterPropertiesSet();

    LdapTemplate ldapTemplate = new LdapTemplate(contextSource);

    byte[] li_byOldpass = encodePassword("oldpass");
    byte[] li_byNewpass = encodePassword("newpass");

    Attribute oldattr = new BasicAttribute("unicodePwd", li_byOldpass);
    Attribute newattr = new BasicAttribute("unicodePwd", li_byNewpass);
    ModificationItem olditem = new   ModificationItem(DirContext.REMOVE_ATTRIBUTE, oldattr);
    ModificationItem newitem = new ModificationItem(DirContext.ADD_ATTRIBUTE, newattr);
    ModificationItem repitem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, newattr);

    ModificationItem[] mods = new ModificationItem[2];
    mods[0] = olditem;
    mods[1] = newitem;

    try
    {
        ldapTemplate.modifyAttributes("CN=Name Surname,OU=Office,DC=example,DC=com", new ModificationItem[] { repitem });   
    }catch(Exception e)
    {
       System.out.println("Error in changing password on Active Directory: " + e.getMessage() );
    }
}

不幸的是它不起作用,这是我得到的错误:


[LDAP:错误代码 32 - 0000208D:NameErr:DSID-0310020A,问题 2001 (NO_OBJECT),数据 0,最佳匹配:'DC=example,DC=com'];


任何帮助将不胜感激

谢谢

最佳答案

这个答案留给子孙后代

检查此链接:http://www.baeldung.com/spring-ldap

代码示例:

    @Autowired
    private LdapTemplate ldapTemplate;

    private static final String BASE_DN = "OU=Metaverse";

    protected void buildDn(UserAd user) {
        Name dn = LdapNameBuilder.newInstance(BASE_DN)
            .add("OU", "Orga_Unit")
            .add("OU", "Orga_Unit")
            .add("CN", "ldap_cn").build();

        DirContextAdapter context = new DirContextAdapter(dn);

        context.setAttributeValues(
           "objectclass", 
           new String[] 
           { "top", 
             "person", 
             "organizationalPerson"});
        context.setAttributeValue("sn", "CREATETEST");
        context.setAttributeValue("userPassword","password");

        ldapTemplate.bind(context);
    }

关于java - 通过 Spring LDAP 更改 Active Directory 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31034908/

相关文章:

java - 字节缓冲区、字符缓冲区、字符串和字符集

java - SWT GridLayout 自动列计数

java - 在子接口(interface)中重写的事务性注解方法是否仍会启动事务

c# - 将用户分配给组,Azure AD Graph Api

c# - 确定 Active Directory 搜索结果中用户的域

java - 有没有办法停止读取特定字符(*)之后的行

Java Spring-passwordEncoder如何生成随 secret 码

spring - 如何在 Spring Tools 4 IDE 中启用 Spring 支持?

spring - Spring Boot Rest API 中的 @Valid 与 @Validated @RequestBody

active-directory - 带通配符的 ldap 查询