Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LDAP example in JAVA

 
Deepti Premkumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards,
Deepti
 
Prashanth Chandra
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic