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 26 27 28 29 30 31
| public static void main(String[] args) {
String LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
Hashtable env = new Hashtable();
env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
env.put( Context.PROVIDER_URL, "ldap://urlLdap:3268/dc=societe,dc=fr");
env.put( Context.SECURITY_AUTHENTICATION, "simple" );
env.put( Context.SECURITY_PRINCIPAL, "CN=userServeur,OU=intra,DC=test,DC=societe,DC=fr" );
env.put( Context.SECURITY_CREDENTIALS, "userServeur_passwd" );
env.put( Context.REFERRAL, "follow" );
DirContext ctx = null;
// connexion au LDAP
try {
ctx = new InitialDirContext( env );
SearchControls ContrainteRecherche = new SearchControls();
ContrainteRecherche.setSearchScope(SearchControls.SUBTREE_SCOPE);
String critere = "(sAMAccountname=user_a_rechercher)";
NamingEnumeration answer = ctx.search("", critere, ContrainteRecherche);
while (answer.hasMore()) {
Binding currentElement = (Binding)answer.next();
System.out.println("getName >>>" + currentElement.getName());
System.out.println("User exists in ADS/LDAP system");
}
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
} |
Partager