How to Query LDAP Search with Plain Java Library

tanut aran
Apr 4, 2024

--

Here is my code that works

 LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(ldapUrls);
contextSource.setBase(ldapBaseDn);
contextSource.setUserDn(username + "@example.com");
contextSource.setPassword(password);
contextSource.afterPropertiesSet();

// LDAP bind happens here
// ldapTemplate.bind have DIFFERENT meaning
// In Java, it is insert data into directory
ldapTemplate.setContextSource(contextSource);

List<LdapUserDetails> users = ldapTemplate.search("", "(cn=" + username + ")", new LdapUserDetailsMapper());

The magic happens here

It works like ORM’s .save() so the configuration is persist into the object.

contextSource.afterPropertiesSet();

Then you can query as you normally do.

Reference

My life saver

https://stackoverflow.com/questions/63170932/spring-ldaptemplate-how-to-modify-the-contextsource-configuration

--

--

tanut aran
tanut aran

Written by tanut aran

Co-founder and Coder at work !

No responses yet