This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Security and the fly likes LDAP example in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "LDAP example in JAVA" Watch "LDAP example in JAVA" New topic
Author

LDAP example in JAVA

Deepti Premkumar
Greenhorn

Joined: Aug 28, 2006
Posts: 5
Hi,

Can anyone give me a complete LDAP example written in JAVA with the steps of execution ?

Regards,
Deepti


Regards,<br />Deepti
Prashanth Chandra
Ranch Hand

Joined: Dec 07, 2005
Posts: 79
Hi

import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes.*;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.net.*;
import java.util.StringTokenizer;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

*****This program is used to retrive the password from LDAP.*******

import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes.*;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.net.*;
import java.util.StringTokenizer;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class ldap
{
public static void main(String a[])
{
String ENTRYDN = "cn="+"abc"+", ou=usermast,o="+"abc.com"+",c=US";// This is rootDN
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://"+"100.100.100.100"+":389");// This is your ldap URL

try{

env.put(Context.SECURITY_PRINCIPAL,"cn=admin, o="+"abc.com"+", c=US");//DN
env.put(Context.SECURITY_CREDENTIALS,"mandiracharu");//This is password

DirContext ctx = new InitialDirContext(env);
Attributes userAttributes = new BasicAttributes(true);
BasicAttribute basicattribute = new BasicAttribute("objectclass","top");
basicattribute.add(1, "person");
basicattribute.add(2, "OpenLDAPperson");
basicattribute.add(3, "connectme");
userAttributes.put(basicattribute);
//This depends upon your LDAP tree structure
userAttributes.put(new BasicAttribute("cn","abc"));
userAttributes.put(new BasicAttribute("userpassword","hello"));
userAttributes.put(new BasicAttribute("uid","abc"));
userAttributes.put(new BasicAttribute("sn","abc"));
userAttributes.put(new BasicAttribute("ispid","0005"));
userAttributes.put(new BasicAttribute("userblocked","no"));
userAttributes.put(new BasicAttribute("parentid","abc"));
userAttributes.put(new BasicAttribute("filename"," "));
userAttributes.put(new BasicAttribute("jpegphoto",""));
ctx.createSubcontext("uid="+"abc"+",ou=usermast,o=abc.com,c=US", userAttributes);//DN
ctx.close();
}catch(Exception e){
e.printStackTrace();
//return "false";
}
//return "true";
}//end of verify

}//end of class


regards
Prashanth
Willian Silva Rodrigues
Greenhorn

Joined: Aug 18, 2011
Posts: 1
I have a very specific problem in access to LDAP.

I need to make an LDAP query, where I tell him to return all user attributes.
I do not know all their names, and changes may occur in naming them, adding new, etc.

I searched on Google and several examples in all cases you need to know the name of the attribute whose value I want to retrieve.

My question is then:
How do I make a query to LDAP using Java code and retrieve the names of all attributes and their values?


_______________________
Willian Silva Rodrigues
http://twitter.com/WillianBR
Brasília-DF / Brazil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LDAP example in JAVA