Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Created January 7, 2014 23:13
Show Gist options
  • Save witoldsz/8308690 to your computer and use it in GitHub Desktop.
Save witoldsz/8308690 to your computer and use it in GitHub Desktop.
package test;
import java.util.Hashtable;
import java.util.Scanner;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.ldap.LdapContext;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
System.out.println(".....................");
String password = null;
while (password == null || password.length() > 0) {
password = new Scanner(System.in).nextLine();
login(password);
}
System.out.println(".....................");
}
public static void login(String password) {
try {
Hashtable hashEnv = new Hashtable();
hashEnv.put(Context.PROVIDER_URL, "ldap://localhost:10389");// SET YOUR SERVER AND STARTING CONTEXT HERE
hashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); //Set Authentication mode
hashEnv.put(Context.SECURITY_PRINCIPAL, "email=mikolaj.pomorski@fs.pl,ou=users,ou=rootGroup,dc=fs,dc=pl"); //Set UserDN
hashEnv.put(Context.SECURITY_CREDENTIALS, password); // SET PASSWORD HERE
hashEnv.put("com.sun.jndi.ldap.connect.pool", "true");//Set connection pooling
hashEnv.put(Context.REFERRAL, "follow");//Set automatic referral chasing on
hashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
hashEnv.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory");
DirContext ctx = new InitialDirContext(hashEnv);
System.out.println("Successful login");
ctx.close();
} catch (NamingException ne) {
System.out.println("Login failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment