修改UID在LDAP存储库

问题描述 投票:2回答:3

我使用LdapTemplate对LDAP库开发应用程序的春天启动了获取用户信息。

我使用的用户电子邮件作为UID,当用户更新他的电子邮件,我必须得更新自己的UID。问题是,下面的工作方法,对所有的人域而不是UID。我得到一个错误:

LDAP: error code 64 - value of naming attribute 'uid' is not present in entry

这是一个片段:

public void updateUser(Person p) throws InvalidNameException {
    Name dn = buildDn(p);
    DirContextOperations context = ldapTemplate.lookupContext(dn);
    LdapMapper.mapToContext(p, context);
    ldapTemplate.modifyAttributes(context);
}

BuildDn:

public Name buildDn(Person p) throws InvalidNameException {
    List<Rdn> lstRdn = new ArrayList<Rdn>();
    lstRdn.add(new Rdn("dc", "priv"));
    lstRdn.add(new Rdn("dc", "com"));
    lstRdn.add(new Rdn("ou", "customers"));
    lstRdn.add(new Rdn("ou", "myusers");
    lstRdn.add(new Rdn("uid", "oldUid"));

    Name name = new LdapName(lstRdn);

    return name;
}

映射器

public static void mapToContext(Person p, DirContextOperations context) {
    context.setAttributeValues("objectclass", new String[] { "top", "person", "inetOrgPerson", "organizationalPerson" });
    context.setAttributeValue("cn", p.getFirstName());
    context.setAttributeValue("sn", p.getLastName());
    context.setAttributeValue("mail", p.getEmail());
    context.setAttributeValue("userPassword", p.getPassword());
    context.setAttributeValue("uid", "NewUid");
}

你有什么想法来修复这个bug?

最好的祝福

java spring spring-boot ldap openldap
3个回答
3
投票

你不能做一个修改的UID场,而不是你将不得不重新命名为更新的UID的纪录。

像这样:

ldapTemplate.rename("uid=oldUid,ou=People,dc=example,dc=com", "uid=newUid,ou=People,dc=example,dc=com")

代替

ldapTemplate.modifyAttributes(context);

资源:https://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/LdapTemplate.html


0
投票
Define the configuratons  for LdapContextSource and LdapTemplate

Name dn = LdapNameBuilder.newInstance().add("uid", uid).build();

Name dnNew = LdapNameBuilder.newInstance().add("uid", newUid).build();

ldapTemplate.rename(dn, dnNew);

这将更新的UID。

https://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/LdapTemplate.html

void    rename(Name oldDn, Name newDn)

移动LDAP树到新位置的条目。


0
投票

ldapTemplate的命名方法的工作不知何故,但我怎么会重命名该属性本身?:

  @Test(expected = SchemaViolationException.class)
  public void testModifyRdnUsingDirContext() {
      LdapName name = LdapNameBuilder.newInstance()
        .add("ou", "people")
        .add("uid", "test")
        .build();
      LdapName newName = LdapNameBuilder.newInstance()
        .add("ou", "people")
        .add("uid", "test1")
        .build();
      // seems disfuctional, because it is not possible to change the value
      // of the uid attribute to the new one beforehand or afterwards. This
      // problem might be unboundID (embedded ldap server) specific.
      ldapTemplate.rename(name, newName);
      DirContextOperations context = ldapTemplate.lookupContext(newName);
      context.setAttributeValue("uid", "test1");
      ldapTemplate.modifyAttributes(context);
    }
© www.soinside.com 2019 - 2024. All rights reserved.