• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

LDAP example in JAVA

 
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
 
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
 
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?

 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic