File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes HOW TO ADD LDAP ENTRIES USING JNDI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "HOW TO ADD LDAP ENTRIES USING JNDI?" Watch "HOW TO ADD LDAP ENTRIES USING JNDI?" New topic
Author

HOW TO ADD LDAP ENTRIES USING JNDI?

suneel suresh
Greenhorn

Joined: Dec 10, 2003
Posts: 9
Hi
i am running OpenLDAP server and i wish to add CN to DC=webselfcare,DC=com. And this i wish to do it from a JNDI Java Program. I am able to read entries from the LDAP server using simple authentication. Now i wish to add and update entries to it, how do i do this??
regards
suneel
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
I used this with IPlanet LDAP long back. Just see if it works for you.

suneel suresh
Greenhorn

Joined: Dec 10, 2003
Posts: 9
thanks karthik
i shall run it and let you know.
regards
suneel
suneel suresh
Greenhorn

Joined: Dec 10, 2003
Posts: 9
karthik i ran the code you gave me but i am getting the following error:Here is my Stdout
Context Sucessfully Initialized
javax.naming.OperationNotSupportedException: [LDAP: error code 53 - referral missing]; remaining name 'uid=defaultuser'
And here is my code:
/*
* Created on Dec 8, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author root
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.NamingException;
import java.util.Hashtable;

public class LdapExampleAdd {
public static void main(String[] args)
{

//Identify service provider to use
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "dc=webselfcare, dc=com");
env.put(Context.SECURITY_CREDENTIALS, "takecare");
try
{
// Create the initial directory context
InitialDirContext initialContext = new InitialDirContext(env);
DirContext dCtx = (DirContext)initialContext;

System.out.println("Context Sucessfully Initialized");


Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("uid", "defaultuser"));
matchAttrs.put(new BasicAttribute("cn", "defaultuser"));
matchAttrs.put(new BasicAttribute("givenname", "defaultuser"));
matchAttrs.put(new BasicAttribute("sn", "defaultuser"));
matchAttrs.put(new BasicAttribute("userpassword", "password"));
matchAttrs.put(new BasicAttribute("objectclass", "top"));
matchAttrs.put(new BasicAttribute("objectclass", "person"));
matchAttrs.put(new BasicAttribute("objectclass", "organizationalPerson"));
matchAttrs.put(new BasicAttribute("objectclass","inetorgperson"));
String name="uid=defaultuser";

InitialDirContext iniDirContext = (InitialDirContext)dCtx;
iniDirContext.bind(name,dCtx,matchAttrs);

iniDirContext.close();
dCtx.close();
}
catch (NamingException ne)
{
System.err.println(ne);
}
catch(Exception e)
{
System.err.println(e);
}
}
}

Whats wrong here karthik?
mod_critical
Greenhorn

Joined: Jan 05, 2005
Posts: 2
suneel, I have a solution to your problem.

I don't know if you have figured this out already but I was having the same problem and I found this poast on Google. I figured out what was wrong and felt i should share =D

The problem is occuring because your LDAP server does not know where to put the new entity. Your string String name="uid=defaultuser" is specifying the DN of the new entry. This DN must be suffixed by the full path to where the entry resides, including and DC entries used in your LDAP server's suffix.

For example, if your suffix is "dc=something,dc=com", and you have created one organization unit with the DN of "ou=users,dc=something,dc=com", you must specify the full DN of your new entry as "uid=defaultuser,ou=users,dc=something,dc=com".

I ran across this problem because I had mistyped the suffix in my DN string =$. I looked up what the error meant from an LDAP perspective and it is related to when you are adding an entry to a slave LDAP server and have not specified an LDAP server to refer to for the rest of the information regarding the suffix you specified.

I hope that this helps you. Good Luck!

-- Thaddeus


Repent, Refactor, Redeem.
mod_critical
Greenhorn

Joined: Jan 05, 2005
Posts: 2
Oh, that post was December 2003. Well, I hope this helps someone then =D
suneel suresh
Greenhorn

Joined: Dec 10, 2003
Posts: 9
Hey thanks for ur message although its a few years later! but i am no longer working...am doing my masters! thanks again
[ March 23, 2005: Message edited by: suneel suresh ]
Monarch Bhojani
Ranch Hand

Joined: Feb 27, 2005
Posts: 46
hello all
i have a similar problem
I am having a servlet in which i wanted to call the class which is adding the entries to the ldap server.my questions are:

1. will i have to create a directory externally or the code which adds will
add itself to the default directory

2. shall i use jndi or normal java class that authenticates and adds is ok

3. if the entry of the user keeps on logging on and off should i every time create entry and authenticate or i should maintain a database

4. which server would be good to run on windows platform

thanks
Monarch
pramu reddi
Greenhorn

Joined: Jun 08, 2005
Posts: 1
hi can any one send reply for Monarch Bhojani question i too have same doubts

thanks
pramu
 
jQuery in Action, 2nd edition
 
subject: HOW TO ADD LDAP ENTRIES USING JNDI?
 
Similar Threads
Authentication mechanism and code procedure from web app to an LDAP server
Authentication mechanism and code procedure from web app to an LDAP server
LDAP connection through JNDI WAS 5
Authentication mechanism and code procedure from web app to an LDAP server
Modify all the attributes