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
| package principal;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main2 {
/**
* @param args
*/
public static void main(String[] args) {
String serverIP = "mon.adresse.ip.de.serveur";
String serverPort = "389";
String serverLogin = "CN=MON LOGIN,OU=0 - Service informatique,OU=UTILISATEURS,DC=MONENTREPRISE,DC=lan";
String serverPass = "monpassword";
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://"+serverIP+":"+serverPort+"/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, serverLogin);
env.put(Context.SECURITY_CREDENTIALS, serverPass);
try {
DirContext ctx = new InitialDirContext(env);
System.out.println("Connexion au serveur : OK");
try {
Attributes attrs = ctx.getAttributes("CN=NOM PRENOM,OU=0 - Service informatique,OU=UTILISATEURS,DC=MONENTREPRISE,DC=lan");
System.out.println("Recuperation de Nom Prenom: OK");
System.out.println(attrs.get("sn"));
System.out.println(attrs.get("name"));
System.out.println(attrs.get("mail"));
System.out.println(attrs.get("department"));
} catch (NamingException e) {
System.out.println("Recuperation de Nom Prenom: KO");
e.printStackTrace();
}
} catch (NamingException e) {
System.out.println("Connexion au serveur : KO");
e.printStackTrace();
}
}
} |
Partager