2

I am trying to insert user details in my LDIF file which is perfectly configured with spring boot because I am able to fetch data even I am able to use LdapTemplate.bind() method.

I have tried various codes and finally, I am able to use ldapTemplate.bind() method but it only binds my entry with LDAP tree means no entry inside ldif file and as I restart my LDAP server this entry losts.

Error:

org.springframework.ldap.InvalidNameException: Invalid name: abcdtestuser@gmail.com; nested exception is javax.naming.InvalidNameException: Invalid name: abcdtestuser@gmail.com
    at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:136)
    at org.springframework.ldap.support.LdapUtils.newLdapName(LdapUtils.java:416)
    at org.springframework.ldap.support.LdapNameBuilder.add(LdapNameBuilder.java:121)
    at in.indg.cdac.ldap.service.LdapService.saveInLdap(LdapService.java:71)
    at in.indg.cdac.ldap.service.LdapService.create(LdapService.java:57)
    at in.indg.cdac.ldap.controller.LdapController.saveUserDetail(LdapController.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

Entity:

@Entry(base="ou=vikaspedia", 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;

Create Method :

    public String create(UserResponsePojo userResponsePojo) {
        try {
            Name dn = LdapNameBuilder
                      .newInstance()
                      .add("ou", "vikaspedia")
                      .add("mail", userResponsePojo.getUid())
//                    .add("cn", userResponsePojo.getUsername())
                      .build();
                    DirContextAdapter context = new DirContextAdapter(dn);

//                  Name id = LdapNameBuilder
//                          .newInstance()
//                          .add(userResponsePojo.getUid())
//                          .build();

                    context.setAttributeValues("objectclass", new String[]{"inetOrgPerson", "person", "top"});
                    context.setAttributeValue("cn", userResponsePojo.getUsername());
                    context.setAttributeValue("sn", userResponsePojo.getLastname());
                    context.setAttributeValue("description", userResponsePojo.getUserrole());
                    context.setAttributeValue("mail", userResponsePojo.getUid());
                    context.setAttributeValue("userPassword", digestSHA(userResponsePojo.getPassword()));

                    ldapTemplate.bind(context);
                    ldapTemplate.create(context);

Maybe, Auto-Increment of Id can resolve the problem but I don't know how can I achieve that as we do with JPA @GeneratedValue(strategy = GeneratedType.IDENTITY)

Any help will be appreciated. Thanks in advance!

Balram Chauhan
  • 171
  • 1
  • 2
  • 14

0 Answers0