Reputation: 1041
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.
Upvotes: 11
Views: 25180
Reputation: 23565
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
}
Upvotes: 14
Reputation: 2209
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>
Upvotes: 7