Testing ldap connection

22,510

Solution 1

Based on the information given it is hard to tell what you know and what you don't know yet. So, I suggest you follow this helpful tutorial at java.net LdapTemplate: LDAP Programming in Java Made Simple and skip the chapters not relevant to you (it's from 2006 but still ok). Spring LDAP referenced in the article is at version 1.3.1 by now.

If you want to go without Spring LDAP for now you can use the following traditional code:

Map<String, String> env = new HashMap<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=jayway,dc=se");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +",ou=system"); // replace with user DN
env.put(Context.SECURITY_CREDENTIALS, password);

DirContext ctx;
try {
   ctx = new InitialDirContext(env);
} catch (NamingException e) {
   // handle
}
try {
   SearchControls controls = new SearchControls();
   controls.setSearchScope( SearchControls.SUBTREE_SCOPE);
   ctx.search( "", "(objectclass=person)", controls);
   // no need to process the results
} catch (NameNotFoundException e) {
   // The base context was not found.
   // Just clean up and exit.
} catch (NamingException e) {
   // exception handling
} finally {
   // close ctx or do Java 7 try-with-resources http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
}

Solution 2

Test the LDAP connection using Spring LDAP authentication:

i.e. with the authenticate() method:

ldapTemplate.authenticate(query, password);

or even better, with getContext() method:

ldapTemplate.getContextSource().getContext(userDn, userPassword));

Catch the org.springframework.ldap.CommunicationException to check if the connection succeeds.

The full code snippet should look like this:

// Create the spring LdapTemplates; i.e. 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'm using spring LDAP 2.3.x version:

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>
Share:
22,510
user1366786
Author by

user1366786

Updated on August 02, 2022

Comments

  • user1366786
    user1366786 almost 2 years

    I want to validate user entered ldap settings. On settings page user enters ldap url, manager dn and password. I have a 'Test settings' button on this page so that user can quickly verify the ldap connection. How to do this easily and quickly?

    Our application using spring security and in the process of adding ldap authentication to it. I am kind of new to java and ldap, so pointing me to right direction is greatly appreciated.

    Thanks.

  • user1366786
    user1366786 over 11 years
    Your example worked well. And also, I tried same with spring's DefaultSpringSecurityContextSource.
  • Dave McLure
    Dave McLure over 6 years
    The article referenced above at the now defunct java.net site (LdapTemplate: LDAP Programming in Java Made Simple) was moved to this URL: community.oracle.com/docs/DOC-983546
  • Jules Kerssemakers
    Jules Kerssemakers over 2 years
    The java.net link in the Answer is a functioning archive.org link. The Oracle-DOC-983546 link redirects to a 'You don't have permission to do that', even after using a (free, unpaid, entry-level) Oracle Single-Sign-On account.