import javax.naming.
NamingEnumeration;
import javax.naming.NamingException;
public class SimpleBindDemo {
public static void main(String[] args) throws NamingException {
if (args.length < 2) {
System.err.println("Usage: java SimpleBindDemo <userDN> <password>");
System.exit(1);
}
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
//env.put(Context.SECURITY_PRINCIPAL,"cn=Manager, ou=People,dc=example,dc=com");
//env.put(Context.SECURITY_CREDENTIALS,"ldap123");
env.put(Context.SECURITY_PRINCIPAL,args[0]);
env.put(Context.SECURITY_CREDENTIALS,args[1]);
try {
Context ctx = new InitialContext(env);
NamingEnumeration enm = ctx.list("");
while (enm.hasMore()) {
System.out.println(enm.next());
}
enm.close();
ctx.close();
} catch (NamingException e) {
System.out.println(e.getMessage());
}
}
}
------------------------------------------------------------------------------------------------------------------------ --
# extended LDIF
#
# LDAPv3
# base <dc=example,dc=com> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
#
example.comdn: dc=example,dc=com
dc: example
objectClass: top
objectClass: domain
# People,
example.comdn: ou=People,dc=example,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
# Group,
example.comdn: ou=Group,dc=example,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
# nagios, People,
example.comdn: uid=nagios,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: nagios
sn: nagios
givenName: nagios
cn: nagios
displayName: nagios
uidNumber: 500
gidNumber: 500
userPassword::
gecos: nagios
loginShell: /bin/bash
homeDirectory: /home/nagios
shadowExpire: -1
shadowFlag: 0
shadowWarning: 7
shadowMin: 0
shadowMax: 99999
shadowLastChange: 15496
# test1, People,
example.comdn: uid=test1,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: test1
sn: test1
givenName: test1
cn: test1
displayName: test1
uidNumber: 501
gidNumber: 501
userPassword::
gecos: test1
loginShell: /bin/bash
homeDirectory: /home/test1
shadowExpire: -1
shadowFlag: 0
shadowWarning: 7
shadowMin: 0
shadowMax: 99999
shadowLastChange: 16447
# test2, People,
example.comdn: uid=test2,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: test2
sn: test2
givenName: test2
cn: test2
displayName: test2
uidNumber: 502
gidNumber: 502
userPassword::
gecos: test2
loginShell: /bin/bash
homeDirectory: /home/test2
shadowExpire: -1
shadowFlag: 0
shadowWarning: 7
shadowMin: 0
shadowMax: 99999
shadowLastChange: 16447
# nagios, Group,
example.comdn: cn=nagios,ou=Group,dc=example,dc=com
objectClass: posixGroup
cn: nagios
gidNumber: 500
# test1, Group,
example.comdn: cn=test1,ou=Group,dc=example,dc=com
objectClass: posixGroup
cn: test1
gidNumber: 501
# test2, Group,
example.comdn: cn=test2,ou=Group,dc=example,dc=com
objectClass: posixGroup
cn: test2
gidNumber: 502
# search result
search: 2
result: 0 Success
# numResponses: 10
# numEntries: 9