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