Yesterday I was looking for a neat and clean solution for authenticating a user via LDAP. Most of the solutions I found read the user from LDAP and compared the credentials with each other. This might be useful if yohave to do other stuff with the User but just for authenticating its to bloated.
So I thought about it it came to my mind: Just connect to the LDAP server with the credentials provided and look if everything is fine:
public boolean authenticate(final String username, final String passwd) {
final Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://myldap:383");
env.put(Context.SECURITY_PRINCIPAL, "CN=" + username + ",CN=Users,DC=Test");
env.put(Context.SECURITY_CREDENTIALS, passwd);
try {
new InitialLdapContext(env, null).close();
} catch (AuthenticationException ae) {
return false;
} catch (NamingException ne) {
throw new RuntimeException(ne);
}
return true;
}
Et voila, user can be authenticated without much effort.
[10:59:25] Daniel Munzinger: 1. Anwendung vom Server entfernen
2. Server neustarten
3. Clean Tomcat Work Directory
4. Anwendung wieder auf den Server deployen
2. Server neustarten
3. Clean Tomcat Work Directory
4. Anwendung wieder auf den Server deployen
Recent Comments