Verify LDAP connectivity using Spring LDAP Authentication:
i.e. using the authenticate () method:
ldapTemplate.authenticate(query, password);
or even better, using the getContext () method:
ldapTemplate.getContextSource().getContext(userDn, userPassword));
Catch org.springframework.ldap.CommunicationException to see if the connection succeeded.
The full code snippet should look like this:
// Create the spring LdapTemplates; ie connections to the source and target ldaps: try { // Note: I'm using the direct LdapTemplate initialization rather than with bean creation (Spring ldap supports both) log.info("Connecting to LDAP " + sourceHost + ":" + sourcePort + "..."); LdapContextSource sourceLdapCtx = new LdapContextSource(); sourceLdapCtx.setUrl("ldap://" + sourceHost + ":" + sourcePort + "/"); sourceLdapCtx.setUserDn(sourceBindAccount); sourceLdapCtx.setPassword(sourcePassword); sourceLdapCtx.setDirObjectFactory(DefaultDirObjectFactory.class); sourceLdapCtx.afterPropertiesSet(); sourceLdapTemplate = new LdapTemplate(sourceLdapCtx); // Authenticate: sourceLdapTemplate.getContextSource().getContext(sourceBindAccount, sourcePassword); } catch (Exception e) { throw new Exception("Failed to connect to LDAP - " + e.getMessage(), e); }
Note. I am using Spring version of LDAP 2.3.x:
<dependency> <groupId>org.springframework.ldap</groupId> <artifactId>spring-ldap-core</artifactId> </dependency>
Naor bar
source share