问题
I am using ldap authentication for my Spring boot project and LDAP is succesfully configured as I am able to authenticate existing users but while I am creating a new user it shows error:
org.springframework.ldap.odm.core.impl.InvalidEntryException: Can't get Id field from Entry org.springframework.ldap.core.DirContextAdapter
I don't know how to generte ID authmatically. Also, I am not able to understand difference between:
ldapTemplate.bind(DirContextAdapter obj)
ldapTemplate.create(DirContextAdapter obj)
ldapRepository.save(user)
```.
Please help me understand this concept and also to register new user entry in LDAP server
User.java
@Entry(base="ou=department", objectClasses = {"inetOrgPerson", "person", "top"})
public class User {
@Id
private Name id;
private @Attribute(name = "mail") String uid;
private @Attribute(name = "cn") String firstName;
private @Attribute(name = "sn") String lastName;
private @Attribute(name = "userPassword") String password;
private @Attribute(name = "description") String userrole;
private @Attribute(name = "mobile") String usermobile
**getters & setters**
public String saveInLdap(UserResponsePojo context) {
try {
User user = new User(
context.getUid(),
context.getUsername(),
context.getLastname(),
digestSHA(context.getPassword()),
context.getUserrole());
Name dn = buildDn(user);
ldapTemplate.bind(dn, null, buildAttributes(user));
userRepository.save(user);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "failed";
}
}
private Name buildDn(User user) {
return LdapNameBuilder.newInstance("o=company")
.add("ou", "department")
.add("mail", user.getUid())
.build();
}
来源:https://stackoverflow.com/questions/59225475/how-to-insert-new-user-or-modify-existing-user-in-ldap-server-using-spring-boot