有人在 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;
}
//...
}