[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

LDAP example

Posted on 2001-09-03
3
Medium Priority
?
672 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 1200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
A solution for Fortify Path Manipulation.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 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