1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import java.util.Hashtable;
public DirContext ActiveDirectory(String serverName, String username, String password) {
DirContext dirContext = null; // returned to the calling program
Hashtable environment = new Hashtable(); // holds parameters that will be passed to the AD server
// Set up the Active Directory environment.
environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, "ldap://" + serverName);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, username);
environment.put(Context.SECURITY_CREDENTIALS, password1);
environment.put(Context.REFERRAL, "follow");
try {
dirContext = new InitialDirContext(environment); // Retrieve a "connection" to the Active Directory server.
} catch (Exception ex) {
System.err.println(ex);
}
return dirContext; |
Partager