Kévin
Kévin

Reputation: 557

Retrieve user information from LDAP with Spring

I need to retrieve user information based on the email of the user. I have see some subject discussing about this but i don't find one with ldapTemplate that respond to the problem.

Actually a user can login through LDAP. I need to retrieve for example the name of the user based on user's mail.

For connection i'm using this, it works :

return ldapTemplate.authenticate(LdapUtils.emptyLdapName(), filter.encode(), password);

To retrieve some information (displayName, department) i tried this without success :

System.out.println(ldapTemplate.search("OU=UTILISATEURS,DC=dig,DC=intra,DC=company,DC=fr", "(&(objectclass=user)([email protected]))", new LdapEntryIdentificationContextMapper()));

Errro : [LDAP: error code 32 - 0000208D: NameErr: DSID-03100288]

Does someone know how can i get those informations ?

Upvotes: 2

Views: 2932

Answers (1)

Kévin
Kévin

Reputation: 557

This is an example with ldapTemplate :

public UserDataDTO retrieveUserInformationFromLDAP(String email) {
        LdapQuery query = LdapQueryBuilder.query().where("objectClass").is("user").and("mail").is(email);
        return ldapTemplate.search(query,
                (AttributesMapper<UserDataDTO>) attributes -> UserDataDTO.builder()
                        .name(attributes.get("givenName").get().toString())
                        .surname(attributes.get("sn").get().toString())
                        .username(attributes.get("displayName").get().toString())
                        .team(attributes.get("department").get().toString())
                        .build())
                .get(0);
    }

Upvotes: 2

Related Questions