How to insert new user or modify existing user in LDAP server using Spring boot ldap

北战南征 提交于 2020-01-16 08:47:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!