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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| /**
* Méthode de connexion à l'annuaire LDAP / AD
* @param PER_LOGIN_LB
* @param PER_PASSWORD_LB
* @return boolean
* @throws Exception
*/
static public boolean connexion_uti_ldap(String PER_LOGIN_LB, String PER_PASSWORD_LB) throws Exception
{
boolean blnConnexionAutorisee = false ;
DirContext ctxtDir = null;
try
{
String LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
//String LDAP_SERVER_URL = "ldap://ldap.enfa.fr:389"; // connexion openLDAP
String LDAP_SERVER_URL = "[ip du serveur]:389"; // Connexion Active Directory Samba 4
//String LDAP_BASE_DN = "dc=local"; // LDAP
String LDAP_BASE_DN = "dc=[notre domaine],dc=lan"; // AD
String LDAP_AUTHENTICATION_MODE = "simple";
String LDAP_REFERRAL_MODE = "follow";
//String LDAP_USER = "uid=" + PER_LOGIN_LB + ",ou=users,dc=enfa.fr,"+LDAP_BASE_DN; // LDAP
//String LDAP_USER = "cn=" + PER_LOGIN_LB + ",ou=people,"+LDAP_BASE_DN; // AD
String LDAP_USER = "samaccountname=" + PER_LOGIN_LB + ",ou=people,"+LDAP_BASE_DN; // AD
String LDAP_PASSWORD = PER_PASSWORD_LB ;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, LDAP_SERVER_URL+"/"+LDAP_BASE_DN);
env.put(Context.SECURITY_AUTHENTICATION, LDAP_AUTHENTICATION_MODE);
env.put(Context.SECURITY_PRINCIPAL, LDAP_USER);
env.put(Context.SECURITY_CREDENTIALS, LDAP_PASSWORD);
env.put(Context.REFERRAL, LDAP_REFERRAL_MODE);
// connexion au LDAP
ctxtDir = new InitialDirContext(env);
blnConnexionAutorisee = true ;
// Fermeture connexion au serveur
try
{
ctxtDir.close();
}
catch (Exception o)
{}
}
catch (Exception z)
{
blnConnexionAutorisee = false ;
if (ctxtDir != null)
{
ctxtDir.close();
}
}
finally
{
try
{
if (ctxtDir != null)
{
ctxtDir.close();
}
}
catch (Exception o)
{}
}
return blnConnexionAutorisee ;
} |
Partager