wood burning stoves*
The moose likes Security and the fly likes LDAP sample program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "LDAP sample program" Watch "LDAP sample program" New topic
Author

LDAP sample program

Kevin Knowles
Ranch Hand

Joined: Dec 29, 2002
Posts: 45
Can anyone provide a simple program demonstrating
the use of LDAP?
Clayton Donley
Author
Greenhorn

Joined: Mar 03, 2003
Posts: 27
Here is an example using JNDI (included with Java 1.3 and above and available for download with older versions of Java). I've thrown in some comments that walk through what it does a bit.
This program does a simple LDAP search for the root entry in the directory. It doesn't print anything fancy, but shows the general process of searching and retrieving results.

(edited by Cindy to format code)
[ March 21, 2003: Message edited by: Cindy Glass ]

Clayton Donley, CTO<br />Octet String, Inc.<br />Phone: +1-847-358-9358 ext. 111<br />Email: clayton.donley@octetstring.com<p>Author: LDAP Programming, Management, and Integration<br />Manning: <a href="http://www.manning.com/donley/" target="_blank" rel="nofollow">http://www.manning.com/donley/</a><br />Amazon: <a href="http://www.amazon.com/exec/obidos/ASIN/1930110405/ref%3Dnosim/searchbyisbn/" target="_blank" rel="nofollow">http://www.amazon.com/exec/obidos/ASIN/1930110405/ref%3Dnosim/searchbyisbn/</a>
Andee Weir
Greenhorn

Joined: Sep 13, 2002
Posts: 12
Sun provide quite a good starter tutorial at Sun's JNDI tutorials
Andee
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
package security;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
public class JDSAuthentication {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://yoda:391";
public static String MY_SEARCHBASE = "dc=isdintegration,dc=com";
public static String MY_FILTER = "jabberID=test1@yoda";
public static String MGR_DN = "cn=directory manager";
public static String MGR_PW = "passwordadmin";
public static void main(String[] args) {
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,INITCTX);
env.put(Context.PROVIDER_URL,MY_HOST);
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,MGR_DN);
env.put(Context.SECURITY_CREDENTIALS,MGR_PW);
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//performs the actual search
//We give it a searchbase, a filter and the contraints containing the scope
//of the search
NamingEnumeration results = ctx.search(MY_SEARCHBASE,MY_FILTER,constraints);
//now stop through the search results
while(results != null && results.hasMore()){
SearchResult sr = (SearchResult)results.next();
String dn = sr.getName();
System.out.println("Distinguished name is "+dn);
Attributes attrs = sr.getAttributes();
for(NamingEnumeration ne = attrs.getAll();ne.hasMoreElements() {
Attribute attr = (Attribute) ne.next();
String attrID = attr.getID();
System.out.println(attrID+" :");
for(Enumeration vals = attr.getAll();vals.hasMoreElements() {
System.out.println("\t"+vals.nextElement());
}
}
System.out.println("\n");
}
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
}
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
With which LDAP Servers do the above samples work?

DirContext.INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory".

Will this property setting in the client program accessing the LDAP database diff from One LDAP Server to another?

thanks,
Vishwa
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Will this property setting in the client program accessing the LDAP database diff from One LDAP Server to another


Yes.


Groovy
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I am using Weblogic 8.1, trying to query/update the LDAP attributes for a user stored in the Weblogic's Embedded LDAP database.


I am able to query the LDAP database, if the used INITIAL_CONTEXT_FACTORY is com.sun.jndi.ldap.LdapCtxFactory(part of JDK1.4.1, I believe).

When I use Weblogic's classes,weblogic.jndi.WLInitialContextFactory
or weblogic.jndi.T3InitialContextFactory , I am getting the following exception.

javax.naming.NotContextException: Not an instance of DirContext

Hence my question, was, can com.sun.jndi.ldap.LdapCtxFactory JNDI driver be used for accesing most of the LDAP databases?
 
jQuery in Action, 2nd edition
 
subject: LDAP sample program
 
Similar Threads
connection to LDAP
IBM Tivoli - LDAP Triggers
Using LDAP and JAAAAS
How to get entries in ldap?
Java App processed partial resultset