Sun Jun 29, 2008 5:11 pm
/**
Sample JNDI application to retrieve group members
*/
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.util.Hashtable;
public class member
{
public static void main(String args[])
{
System.out.println("here is 1");
Hashtable env= new Hashtable();
//cn=Manager,ou=People,dc=centos5,dc=com
//uid=root,ou=People,dc=centos5,dc=com
//uid=root,ou=People,dc=centos5,dc=com
env.put(Context.SECURITY_AUTHENTICATION,"Simple");
env.put(Context.SECURITY_PRINCIPAL,"dc=development,dc=local");//User
env.put(Context.SECURITY_CREDENTIALS, "kali");//Password
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://10.10.1.1:389/dc=development,dc=local");
System.out.println("here is 2");
try {
//Create the initial directory context
System.out.println("here is 111");
LdapContext ctx = new InitialLdapContext(env,null);
System.out.println("here is 222");
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
String searchFilter = "(cn=HAL*)";
//Specify the Base for the search
String searchBase = "dc=centos5,dc=com";
//String searchBase = "o=centos5.com";
//initialize counter to total the group members
int totalResults = 0;
//Specify the attributes to return
String returnedAtts[]={"member"};
System.out.println("here is 3");
searchCtls.setReturningAttributes(returnedAtts);
System.out.println("here is 4");
//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
System.out.println("here is 5");
//Loop through the search results
/*while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());
//Print out the members
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
for (NamingEnumeration ae = attrs.getAll();ae.hasMore();) {
Attribute attr = (Attribute)ae.next();
System.out.println("Attribute: " + attr.getID());
for (NamingEnumeration e = attr.getAll();e.hasMore();totalResults++) {
System.out.println(" " + totalResults + ". " + e.next());
}
}
}
catch (NamingException e)
{
System.err.println("Problem listing members: " + e);
e.printStackTrace();
}
}
}*/
System.out.println("Total members: " + totalResults);
ctx.close();
}
catch (NamingException e) {
System.err.println("Problem searching directory: " + e);
}
}
}
Mon Jun 30, 2008 12:11 am
Mon Jun 30, 2008 9:35 am
Mon Jun 30, 2008 8:50 pm
LdapContext ctx = new InitialLdapContext(env,null);
Wed Feb 25, 2009 1:59 pm
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
public class TestLdapScript {
public static void main(String[] args) {
String userName = "username";
String passWord = "password";
try { Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"LDAP://xx.xxxxx.xx:389"); //replace with your server URL/IP
//only DIGEST-MD5 works with our Windows Active Directory
env.put(Context.SECURITY_AUTHENTICATION,"DIGEST-MD5"); //No other SALS worked with me
env.put(Context.SECURITY_PRINCIPAL,userName); // specify the username ONLY to let Microsoft Happy
env.put(Context.SECURITY_CREDENTIALS, passWord); //the password
DirContext ctx = new InitialDirContext(env);
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"cn","DisplayName"};
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
//String searchFilter = "(&(ObjectClass=Person)(!(ObjectClass=user)))";
//String searchFilter = "(&(objectClass=user)(&(objectClass=Person)(!(userAccountControl=514))))";
String searchFilter = "(&(objectClass=person)(CN=))";
//Specify the Base for the search
String searchBase = "DC=xxxx,DC=net";
//initialize counter to total the results
int totalResults = 0;
// 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();
totalResults++;
System.out.println(">>>" + sr.getName());
// Print out some of the attributes, catch the exception if the attributes have no values
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
System.out.println(" surname: " + attrs.get("cn").get());
System.out.println(" firstname: " + attrs.get("DisplayName").get());
}
catch (NullPointerException e) {
System.out.println("Errors listing attributes: " + e);
}
}
}
ctx.close();
} catch(NamingException ne)
{ System.out.println("Error authenticating user:");
System.out.println(ne.getMessage());
return;
} //if no exception, the user is already authenticated.
System.out.println("OK, successfully authenticating user");
}
}
Wed Feb 25, 2009 2:47 pm
Thu Feb 26, 2009 6:09 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.