一.使用OpenDs启动LDAP服务。参阅:http://blog.csdn.net/kunshan_shenbin/archive/2007/12/20/1956093.aspx
二.导入测试数据入LDAP服务器。(可使用Softerra LDAP Administrator 3.5进行导入)数据如下,文件命名为sample.ldif:
dn: dc=example,dc=com
objectclass: top
objectclass: domain
dc: example
dn: o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: organization
o: MyCorp
dn: ou=Groups,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: Groups
dn: cn=Administrators,ou=Groups,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: Administrators
uniqueMember: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
uniqueMember: uid=bcrane,ou=People,o=MyCorp,dc=example,dc=com
dn: ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: People
dn: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: abrown
givenName: Aaron
sn: Brown
cn: Aaron Brown
mail: abrown@mycorp.com
userPassword: abrown
facsimiletelephonenumber: 666
dn: uid=bcrane,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: bcrane
givenName: Brian
sn: Crane
cn: Brian Crane
mail: bcrane@mycorp.com
userPassword: bcrane
secretary: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
dn: uid=cdaniels,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: cdaniels
givenName: Charlene
sn: Daniels
cn: Charlene Daniels
mail: cdaniels@mycorp.com
userPassword: cdaniels
secretary: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: domain
dc: example
dn: o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: organization
o: MyCorp
dn: ou=Groups,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: Groups
dn: cn=Administrators,ou=Groups,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: Administrators
uniqueMember: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
uniqueMember: uid=bcrane,ou=People,o=MyCorp,dc=example,dc=com
dn: ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: People
dn: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: abrown
givenName: Aaron
sn: Brown
cn: Aaron Brown
mail: abrown@mycorp.com
userPassword: abrown
facsimiletelephonenumber: 666
dn: uid=bcrane,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: bcrane
givenName: Brian
sn: Crane
cn: Brian Crane
mail: bcrane@mycorp.com
userPassword: bcrane
secretary: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
dn: uid=cdaniels,ou=People,o=MyCorp,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: cdaniels
givenName: Charlene
sn: Daniels
cn: Charlene Daniels
mail: cdaniels@mycorp.com
userPassword: cdaniels
secretary: uid=abrown,ou=People,o=MyCorp,dc=example,dc=com
导入后数据结构如下:
Java代码如下:
package
ldap;
import java.util.Properties;
import javax.naming. * ;
import javax.naming.ldap. * ;
import javax.naming.directory. * ;
public class LdapClient ... {
public static void main(String[] args) ...{
Properties env = new Properties();
String adminName = "cn=Directory Manager";
String adminPassword = "aimsora";
String ldapURL = "LDAP://192.168.2.148:389";
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
//set security credentials, note using simple cleartext authentication
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPassword);
//connect to my domain controller
env.put(Context.PROVIDER_URL, ldapURL);
try ...{
//Create the initial directory context
LdapContext ctx = new InitialLdapContext(env, null);
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
//String searchFilter = "(&(objectCategory=person)(objectClass=user)(name=004*))";
String searchFilter = "(sn=Brown)";
//Specify the Base for the search
String searchBase = "ou=People,o=MyCorp,dc=example,dc=com";
//initialize counter to total the group members
int totalResults = 0;
//Specify the attributes to return
String returnedAtts[] = ...{"mail"};
searchCtls.setReturningAttributes(returnedAtts);
//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,
searchCtls);
//Loop through the search results
while (answer.hasMoreElements()) ...{
SearchResult sr = (SearchResult) answer.next();
System.out.println(">>>" + sr.getName());
//Print out the groups
Attributes attrs = sr.getAttributes();
if (attrs != null) ...{
try ...{
for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) ...{
Attribute attr = (Attribute) ae.next();
System.out.println("AttributeID: " + attr.getID());
for (NamingEnumeration e = attr.getAll(); e.hasMore();
totalResults++) ...{
System.out.println("Attributes:"+e.next());
}
}
} catch (NamingException e) ...{
e.printStackTrace();
System.err.println("Problem listing membership: " + e);
}
}
}
System.out.println("Total groups: " + totalResults);
ctx.close();
}catch (NamingException e) ...{
e.printStackTrace();
System.err.println("Problem searching directory: " + e);
}
}
}
import java.util.Properties;
import javax.naming. * ;
import javax.naming.ldap. * ;
import javax.naming.directory. * ;
public class LdapClient ... {
public static void main(String[] args) ...{
Properties env = new Properties();
String adminName = "cn=Directory Manager";
String adminPassword = "aimsora";
String ldapURL = "LDAP://192.168.2.148:389";
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
//set security credentials, note using simple cleartext authentication
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPassword);
//connect to my domain controller
env.put(Context.PROVIDER_URL, ldapURL);
try ...{
//Create the initial directory context
LdapContext ctx = new InitialLdapContext(env, null);
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
//String searchFilter = "(&(objectCategory=person)(objectClass=user)(name=004*))";
String searchFilter = "(sn=Brown)";
//Specify the Base for the search
String searchBase = "ou=People,o=MyCorp,dc=example,dc=com";
//initialize counter to total the group members
int totalResults = 0;
//Specify the attributes to return
String returnedAtts[] = ...{"mail"};
searchCtls.setReturningAttributes(returnedAtts);
//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,
searchCtls);
//Loop through the search results
while (answer.hasMoreElements()) ...{
SearchResult sr = (SearchResult) answer.next();
System.out.println(">>>" + sr.getName());
//Print out the groups
Attributes attrs = sr.getAttributes();
if (attrs != null) ...{
try ...{
for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) ...{
Attribute attr = (Attribute) ae.next();
System.out.println("AttributeID: " + attr.getID());
for (NamingEnumeration e = attr.getAll(); e.hasMore();
totalResults++) ...{
System.out.println("Attributes:"+e.next());
}
}
} catch (NamingException e) ...{
e.printStackTrace();
System.err.println("Problem listing membership: " + e);
}
}
}
System.out.println("Total groups: " + totalResults);
ctx.close();
}catch (NamingException e) ...{
e.printStackTrace();
System.err.println("Problem searching directory: " + e);
}
}
}
输出结果:
>>>uid=abrown
AttributeID: mail
Attributes:abrown@mycorp.com
Total groups: 1
AttributeID: mail
Attributes:abrown@mycorp.com
Total groups: 1