Here you go, just a quick and dirty cut and paste to test a simple bind non-ssl:
pass your user dn and password on the command line. Use empty quotes for an empty password.
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.net.*;
public class TestBind {
final static String host = “”;
final static String port = “389”;
public static void main(String[] args) {
DirContext ctx = null;
String baseDN = “”;
String userDN = args[0];
String password = args[1];
try {
// See if the user authenticates.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, “com.sun.jndi.ldap.LdapCtxFactory”);
env.put(Context.PROVIDER_URL, getProviderURL(baseDN));
//if (sslEnabled) {
// env.put(“java.naming.ldap.factory.socket”, “org.jivesoftware.util.SimpleSSLSocketFactory”);
// env.put(Context.SECURITY_PROTOCOL, “ssl”);
//}
env.put(Context.SECURITY_AUTHENTICATION, “simple”);
System.out.println("Binding as: " +userDN + “,” + baseDN);
env.put(Context.SECURITY_PRINCIPAL, userDN + “,” + baseDN);
env.put(Context.SECURITY_CREDENTIALS, password);
ctx = new InitialDirContext(env);
System.out.println("… context created successfully, returning.");
System.exit(0);
}
catch (NamingException ne) {
System.err.println("No bind: " + ne.getMessage());
}
}
private static String getProviderURL(String baseDN) {
String ldapURL = “”;
try {
// Create a correctly-encoded ldap URL for the PROVIDER_URL
ldapURL = “ldap://” + host + “:” + port + “/” +
URLEncoder.encode(baseDN, “UTF-8”);
// The java.net.URLEncoder class encodes spaces as +, but they need to be %20
ldapURL = ldapURL.replaceAll("
+", “%20”);
}
catch (java.io.UnsupportedEncodingException e) {
// UTF-8 is not supported, fall back to using raw baseDN
ldapURL = “ldap://” + host + “:” + port + “/” + baseDN;
}
System.out.println("ldapURL = " + ldapURL);
return ldapURL;
}
}