Solved

LDAP example

Posted on 2001-09-03
3
641 Views
Last Modified: 2013-11-24
Hi

I am looking for a quick ldap example on how to store something in a directory tree in LDAP.

Lets say I have the following

User = directory
     - name  = value
     - password = value

Can anyone give me quick example on how to do this in LDAP using JNDI?

Thanks
Marius
0
Comment
Question by:Veroland
3 Comments
 
LVL 1

Accepted Solution

by:
chauhanvinit earned 300 total points
ID: 6452401
Hi,
Try this code ,
Make sure u have ldap provider in yur classpath.
You have to download it from sun site.

Initial context would depend upon yur Ldap server.
You will have to provide these values while building an initial context like Server adds, Base DN, authentication (if the server does).




import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;


class Modattrs {
  public static void main(String[] args) {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY,          "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://severname");

    try {
     // Create the initial context
     DirContext ctx = new InitialDirContext(env);
     String name = "cn=Ted Geisel, ou=People";

     // Save original attributes
     Attributes orig = ctx.getAttributes(name);

     // Specify the changes to make
     ModificationItem[] mods =new ModificationItem[3];

     mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,new BasicAttribute("dir", "\user\home"));

     mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
          new BasicAttribute("cn", "chauhanvinit"));
         
        mods[2] = new ModificationItem(DirContext.ADD_ATTRIBUTE,new BasicAttribute("passwd", "empty"));
         ctx.modifyAttributes(name, mods);
        // Check attributes
     System.out.println("**** new attributes *****");
     GetattrsAll.printAttrs(ctx.getAttributes(name));

     // Revert changes
     ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

     // Check that the attributes got restored
     System.out.println("**** reverted to original attributes *****");
     GetattrsAll.printAttrs(ctx.getAttributes(name));

     // Close the context when we're done
         ctx.close();
     } catch (Exception e) {
         e.printStackTrace();
     }
    }
}

yu will get detailed explaination at :-
http://java.sun.com/products/jndi/tutorial/basics/directory/modattrs.html


Bye for now.
0
 
LVL 5

Expert Comment

by:vemul
ID: 7766033
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to chauhanvinit

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7847339
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now