How to Query LDAP Search with Plain Java Library
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