File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes Ldap newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Ldap newbie" Watch "Ldap newbie" New topic

Ldap newbie

Steve Sutton

Joined: Aug 06, 2003
Posts: 7
I am new to ldap and I am having a hard time finding examples of what I need to accomplish. I am performing a search on a user and getting an attribute under that user. I want to get the value of that attribute and pass that along. Here is the code that I have.
DirContext ctx = null;
String hostURL = "ldap://";
String passWord = "lwcsvc";

// Create a Hashtable object.
Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, hostURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple" );
env.put(Context.SECURITY_PRINCIPAL, "cn=lwcsvc,ou=svcacct,dc=tads,dc=nwa,dc=com" );
env.put(Context.SECURITY_CREDENTIALS, passWord );

// Create the initial directory context

System.out.println(" Binding to the context");

ctx = new InitialDirContext(env);
catch (NamingException e)
System.err.println("Problem with Bind:" + e);

System.out.println("Successful Bind");
String baseDN = "CN=Users,DC=tads,DC=xxx,DC=com";
String searchFilter = "(CN=" + empID +")";
String[] attrID= {"memberOf"};

SearchControls ctls = new SearchControls();

NamingEnumeration answer =, searchFilter, ctls);
while (answer.hasMore())
SearchResult sr = (SearchResult);
System.out.println(sr.toString() + "\n");
catch (Exception e)
System.out.println("Could not find Group for empID: " +empID +e);
The result is:
"CN=c02188: null:null:{memberof
=memberOf: CN=monitorRecords,OU=groups,DC=tads,DC=xxx,DC=com}"
What I would really like to do is just retrieve the value of 'monitorRecords' instead of the entire string. How do I use the getAttribute function to do this after the search.
I agree. Here's the link:
subject: Ldap newbie
It's not a secret anymore!