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
| mport javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
/**
* Demonstrates how to create an initial context to an LDAP server
* using simple authentication.
*
* usage: java Simple
*/
class Simple {
public static void main(String[] args) {
// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=Employe, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
System.out.println(ctx.lookup("ou=Employe"));
// do something useful with ctx
String[] attrIDs = {"matricule","nom","prenom","titre","fonction","login","password","email","connecte"};
// Get the attributes requested
Attributes answer = ctx.getAttributes("cn= S. User, ou=Employe",attrIDs);
For (NamingEnumeration ae = answer.getAll(); ae.hasMore();){
Attribute attr = (Attribute)ae.next();
System.out.println("attribute:" + attr.getID());
/* print each value */
For (NamingEnumeration e = attr.getAll(); e.hasMore();){
System.out.println("value:" + e.next());
}
}
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
} |
Partager