使用LdapTemplate为Ldap posixAccount生成uidNumber

时间:2013-06-14 14:08:36

标签: java ldap spring-ldap

是否有人在Ldap中使用posixAccount创建时遇到此错误?

 javax.naming.directory.SchemaViolationException: [LDAP: error code 65 - object class 'posixAccount' requires attribute 'uidNumber']

是否有一种智能方法可以生成一个唯一的UidNumber,将创建委托给Ldap而不是注意找出唯一性? (就像SQL Server中的标识列一样)

由于

这里是我使用的代码:

public class LdapService {

//...
private LdapTemplate ldapTemplate;

public UserInfo save(final UserInfo user) {
    Name dn = buildDn(user);
    ldapTemplate.bind(dn, null, buildUserAttributes(user));

    // Update Groups
    for (String group : user.getGroups()) {
        try {
            DistinguishedName groupDn = new DistinguishedName();
            groupDn.add("ou", "groups");
            groupDn.add("cn", group);
            DirContextOperations context = ldapTemplate
                    .lookupContext(groupDn);
            context.addAttributeValue("memberUid", user.getUid());
            ldapTemplate.modifyAttributes(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return user;
}

private Attributes buildUserAttributes(final UserInfo user) {
    Attributes attrs = new BasicAttributes();
    BasicAttribute ocattr = new BasicAttribute("objectclass");
    ocattr.add("top");
    ocattr.add("inetOrgPerson");
    ocattr.add("posixAccount");
    attrs.put(ocattr);
    attrs.put("givenName", user.getName());
    attrs.put("sn", user.getSurname());
    if (user.getDisplayName() != null)
        attrs.put("cn", user.getDisplayName());
    attrs.put("userPassword", "{SHA}" + this.encrypt(user.getPassword()));
    attrs.put("mail", user.getEmail());

    return attrs;
}
//...
}

1 个答案:

答案 0 :(得分:0)

如果您的服务器支持修改 - 增量请求控制,则LDAP客户端可以使用它来在一个原子操作中增加整数。另见:LDAP: Modify-Increment Extension

相关问题