Use Java to access data in the LDAP server

Source: Internet
Author: User
Tags ldap ldap search filter

1. Use opends to start the LDAP service. See http://blog.csdn.net/kunshan_shenbin/archive/2007/12/20/1956093.aspx

2. Import the test data to the LDAP server. (You can use softerra LDAP administrator 3.5 to import data.) The file named sample. ldif is as follows:

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

The imported data structure is as follows:

The Java code is as follows:

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 );
}
}
}

Output result:

>>> Uid = abrown
Attributeid: Mail
Attributes: abrown@mycorp.com
Total groups: 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.