Skip to content

Instantly share code, notes, and snippets.

@akkidas
Created April 5, 2016 16:48
Show Gist options
  • Save akkidas/d1d10b65e97b77a8ec1340c39c5418c9 to your computer and use it in GitHub Desktop.
Save akkidas/d1d10b65e97b77a8ec1340c39c5418c9 to your computer and use it in GitHub Desktop.
// Setup the LDAP client (normally done via Spring context file).
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://localhost:389");
contextSource.setBase("DC=COMPANY,DC=COM");
contextSource.setUserDn("user@test.com");
contextSource.setPassword("password");
contextSource.afterPropertiesSet();
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
ldapTemplate.afterPropertiesSet();
// Perform the authentication.
Filter filter = new EqualsFilter("sAccountName", "testuser");
boolean authed = ldapTemplate.authenticate("ou=people,ou=myrealm",
filter.encode(),
"user-password");
// Display the results.
System.out.println("Authenticated: " + authed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment