I want to get attribute on my active directory with Java (Eclipse).
I found this code :
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL,
"cn=S. User, ou=NewHires, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");
// Create the initial context
DirContext ctx = new InitialDirContext(env);
but I have start with this to know if there is a connection:
String ldapUrl = "ldap://"+serverAddress+":389";
//Prepare the environment with the username and password.
Hashtable env = new Hashtable();
DirContext ctx = null;
env.put(Context.SECURITY_PRINCIPAL, DOMAIN+username);
//env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
ctx = LdapCtxFactory.getLdapCtxInstance(ldapUrl, env); // To test the connection
So how can I create an InitialDirContext ?
What should I put in env.put(Context.INITIAL_CONTEXT_FACTORY, "?????????") for that works ?
Thanks a lot.
In the example you found, the connection is established when you do:
For this to work, you need to have all the required environment properties set, like
The context factory is what Java is going to use to create that connection. If you don’t specify it, java will use the default which is
com.sun.jndi.ldap.LdapCtxFactory, if I’m not mistaken.