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 32 33
|
DirContext ldapContext = null;
String ldap_server="127.0.0.1"; //+ ":636"
//le port par défaut n'a pas besoin d'être précisé.
//127.0.0.1 pour localhost ;)
String global_dc="dc=ton_dc,dc=com"; //ton_dc à changer
String ou_dn="ou=ton_ou,"+global_dc; //ton_ou à changer, en fonction de l'architecture de ton annuaire
String admin_dn="cn="+getUserName()+","+global_dc; //Identifiant de connexion à l'annuaire
String admin_pwd=getPassword();
String user_uid="user_a_tester"; //Identifiant à tester dans l'annuaire
String user_pwd="pass_a_tester";
String user_dn="uid="+user_uid+","+ou_dn;
try{
Hashtable<String,String> ldapEnv = new Hashtable<String,String>();
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, "ldap://" + ldap_server);
ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
//ldapEnv.put(Context.SECURITY_PRINCIPAL, admin_dn);
//ldapEnv.put(Context.SECURITY_CREDENTIALS, admin_pwd);
ldapEnv.put(Context.SECURITY_PRINCIPAL, user_dn);
ldapEnv.put(Context.SECURITY_CREDENTIALS, user_pwd);
//ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl");
ldapContext = new InitialDirContext(ldapEnv);
System.out.println("LDAP : Bind Ok = "+user_uid);
return true;
}catch (Exception e){
System.out.println("LDAP : Bind Error = "+e);
return false;
} |
Partager