Skip to content

Instantly share code, notes, and snippets.

@buggtb
Created August 20, 2016 17:52
Show Gist options
  • Save buggtb/4084f76484c771dd1aca14570c932e24 to your computer and use it in GitHub Desktop.
Save buggtb/4084f76484c771dd1aca14570c932e24 to your computer and use it in GitHub Desktop.
package com.meteorite;
import org.ldaptive.*;
import org.ldaptive.auth.*;
import org.ldaptive.control.ResponseControl;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws LdapException {
ConnectionConfig connConfig = new ConnectionConfig("ldap://ldaperserver.com:3268");
connConfig.setUseStartTLS(false);
connConfig.setConnectionInitializer(
new BindConnectionInitializer(args[0], new Credential(args[1])));
// use a search dn resolver
SearchDnResolver dnResolver = new SearchDnResolver(new DefaultConnectionFactory(connConfig));
dnResolver.setBaseDn("DC=mydc,DC=com");
dnResolver.setUserFilter("(mail={user})");
dnResolver.setSubtreeSearch(true);
// perform a bind for password validation
BindAuthenticationHandler authHandler = new BindAuthenticationHandler(new DefaultConnectionFactory(connConfig));
Authenticator auth = new Authenticator(dnResolver, authHandler);
AuthenticationResponse response = auth.authenticate(
new AuthenticationRequest(args[0], new Credential(args[1]), new String[] {"memberOf", "sn"}));
if (response.getResult()) { // authentication succeeded
LdapEntry entry = response.getLdapEntry(); // read mail and sn attributes
System.out.println("LDAP ENTRY");
System.out.println(entry.toString());
} else { // authentication failed
String msg = response.getMessage(); // read the failure message
ResponseControl[] ctls = response.getControls(); // read any response controls
System.out.println(msg);
if(ctls!=null) {
for (ResponseControl r : ctls) {
System.out.println(r.toString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment