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
|
public static void main(String[] args)
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
// LDAP url de la forme : ldap://server:port/
env.put(Context.PROVIDER_URL, HOST);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// Login à renseigner...
env.put(Context.SECURITY_PRINCIPAL, LOGIN);
// Password à renseigner...
env.put(Context.SECURITY_CREDENTIALS, PASSWORD);
try
{
DirContext ctx = new InitialDirContext(env);
listGroups(ctx, "O=Mon organisation");
}
catch (NamingException e)
{
e.printStackTrace();
}
}
public static void listGroups(DirContext ctx, String root)
throws NamingException
{
NamingEnumeration nEnum = ctx.list(root);
while (nEnum != null && nEnum.hasMoreElements())
{
NameClassPair ncp = (NameClassPair) nEnum.nextElement();
String group = ncp.getName();
System.out.println(group.substring(group.indexOf("=") + 1));
// On liste les éventuels attributs
listAttributes(ctx, group + "," + root);
// Appel récursif
listGroups(ctx, group + "," + root);
}
}
public static void listAttributes(DirContext ctx, String root)
throws NamingException
{
Attributes attributes = ctx.getAttributes(root);
NamingEnumeration nEnum = attributes.getAll();
while (nEnum != null && nEnum.hasMoreElements())
{
Attribute attribute = (Attribute) nEnum.nextElement();
System.out.println("\t\t" + attribute);
}
} |
Partager