Access LDAP using Java

Source: Internet
Author: User

Import Java. util. properties;
Import Javax. naming. * ;
Import Javax. Naming. LDAP. * ;
Import Javax. Naming. directory. * ;
Public   Class Client {
Public   Static   Void Main (string [] ARGs ){
Properties env =   New Properties ();
String adminname =   " Usish \ Derek " ;
String adminpassword =   " Dada " ;
String ldapurl =   " LDAP: // 192.168.40.42: 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 {< br> /// 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 *))";

// Specify the base for the search
String searchbase =   " Ou = usish, Dc = usish, Dc = com, Dc = Cn " ;
// Initialize counter to total the group members
Int Totalresults =   0 ;
// Specify the attributes to return
String returnedatts [] = { " Memberof " };
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 );
}
}
}

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.