1
votes

I am using ldapTemplate of Spring. In my ldaputility.java i am doing like this:

public Object findUser(String username) {
System.out.println("inside find USer with username : "+username);
Object object=ldapTemplate.lookup("uid=xyz,cn=users,o=companyName", 
new AttributesMapper<Object>()//GETTING ERROR AT THIS LINE
{

    @Override
    public User mapFromAttributes(Attributes attrs) throws NamingException {
        User user = new User();
    user.setFullName((String)attrs.get("cn").get());
    user.setLastName((String)attrs.get("sn").get());
    user.setPassword((String)attrs.get("password").get());

    return user;
    }
    });
    return object;
    }

in my ldif i am doing something like this:

dn: o=companyName
objectclass: domain
objectclass: top

dn: cn=users,o=companyName
objectclass: container
objectclass: top
cn: users

dn: cn=groups,o=companyName
objectclass: top
objectclass: container
cn: groups

dn: uid=xyz,cn=users,o=companyName
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: xyz
userpassword: xyz123
sn: xyz
givenName: xyz
cn: xyz abc

When controle comes on ldapTemplate.lookup() , i am getting following error

    org.springframework.ldap.InvalidNameException: uid=xyz,cn=users,o=companyName: [LDAP: error code 34 - Invalid DN Syntax]; 
nested exception is javax.naming.InvalidNameException: uid=xyz,cn=users,o=companyName: [LDAP: error code 34 - Invalid DN Syntax]; 
remaining name 'uid=xyz,cn=users,o=companyName

Searched a lot but could not able to resolve this. Someone please help me on this.

1

1 Answers

2
votes

The exception indicates invalid syntax in the distinguished name. The DN syntax - particularly regarding encoding of special characters - is quite complex so you should typically avoid constructing distinguished names using string concatenation (if that's what you're doing).

Consider using LdapNameBuilder for building distinguished names.