Testing the ldap connection

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

Our application uses spring security and in the process of adding ldap authentication to it. I am partly new to java and ldap, so it is very important to evaluate the direction to the right.

Thanks.

+8
java spring spring-security ldap
source share
2 answers

Based on the information given, it is difficult to say what you know and what you still do not know. So, I suggest you follow this useful guide on java.net LdapTemplate: LDAP Programming in Java Made Simple and skip chapters that are not relevant to you (this is from 2006, but still good). Spring LDAP , mentioned in the article, is in version 1.3.1.

If you want to do without Spring LDAP, 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 } 
+9
source share

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> 
+1
source share

All Articles