JNDI/LDAP newbie

Discussion in 'Java' started by blm14@columbia.edu, May 2, 2007.

  1. Guest

    Hey everyone! So basically I am trying to run a name search using an
    LDAP server, let's call it ldap.xxx.com. I've never done anything with
    JNDI or LDAP before, and what I need to do essentially is this:

    String firstname = "Bob";
    String lastname = "Smith";

    //get LDAP connection to ldap.xxx.com

    Object results = ldapServerObj.query(firstname,lastname)

    I have no familiarity with the classes or objects involved in doing
    this so I left it generic. :) Any help appreciated...
    , May 2, 2007
    #1
    1. Advertising

  2. Nigel Wade Guest

    wrote:

    > Hey everyone! So basically I am trying to run a name search using an
    > LDAP server, let's call it ldap.xxx.com. I've never done anything with
    > JNDI or LDAP before, and what I need to do essentially is this:
    >
    > String firstname = "Bob";
    > String lastname = "Smith";
    >
    > //get LDAP connection to ldap.xxx.com
    >
    > Object results = ldapServerObj.query(firstname,lastname)
    >
    > I have no familiarity with the classes or objects involved in doing
    > this so I left it generic. :) Any help appreciated...


    You could start here:

    http://java.sun.com/products/jndi/tutorial/index.html

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, May 2, 2007
    #2
    1. Advertising

  3. wrote:
    > Hey everyone! So basically I am trying to run a name search using an
    > LDAP server, let's call it ldap.xxx.com. I've never done anything with
    > JNDI or LDAP before, and what I need to do essentially is this:
    >
    > String firstname = "Bob";
    > String lastname = "Smith";
    >
    > //get LDAP connection to ldap.xxx.com
    >
    > Object results = ldapServerObj.query(firstname,lastname)
    >
    > I have no familiarity with the classes or objects involved in doing
    > this so I left it generic. :) Any help appreciated...
    >


    Well it won't quite be that easy. I'd suggest before trying to interface
    with an LDAP server programmatically you read about the protocol itself
    and how directory servers operate. Ideally you should attempt to
    interface with a directory server using the native tools for a
    particular directory server.

    Short of that you need to do the following:
    1. Determine the type of LDAP directory server you will be interface
    with: Active Directory, OpenLDAP, Novell eDirectory, Oracle Internet
    Directory, Sun One, etc. (there are a couple others but they aren't
    popular).

    2. As long as you only need to do searches on the objects in the tree
    and not the schema then you can actually use JNDI (from the JDK) for at
    least Active Directory, OpenLDAP, and Sun One. My app uses JNDI for
    those 3 for read-only access. If you need to make changes then JNDI will
    only work for Active Directory and OpenLDAP. To make changes to Sun One
    you have to use the netscape Java API. There is a JNDI provider that may
    work (I haven't tried it yet though).

    3. Within your actual code you need to setup a Hashtable that contains
    values you will use to connect to the server:
    ldapEnv.put(Context.SECURITY_AUTHENTICATION,"simple");
    ldapEnv.put(Context.SECURITY_PRINCIPAL,username);
    ldapEnv.put(Context.SECURITY_CREDENTIALS,password);
    ldapEnv.put(Context.PROVIDER_URL, "ldap://" +
    host + ":" + port);

    4. Setup an InitialLdapContext using the Hashtable:
    ctx = new InitialLdapContext(ldapEnv,null);

    5. Then you need to setup your search parameters:
    attribs is a comma-delimited list of attributes you want returned in
    each object of the search results (e.g. givenName, sn)

    public Vector<SearchResult> search(String base,
    int resultLimit,
    int searchTimeLimit,
    int scope, String filter,
    String attribs) throws Exception {
    NamingEnumeration results = null;
    SortControl reqControl = null;

    String[] attrs = null;
    attrs = attribs.split(",");

    SearchControls searchControls = new SearchControls();
    searchControls.setReturningAttributes(attrs);
    searchControls.setSearchScope(scope);
    searchControls.setTimeLimit(searchTimeLimit);
    searchControls.setCountLimit(resultLimit);
    try {
    reqControl = new SortControl("cn",true);
    } catch (IOException io) {}
    ctx.setRequestControls(new Control[] {reqControl} );
    results = ctx.search(base, filter, searchControls);
    Vector<SearchResult> sortedResults = new Vector<SearchResult>();

    while (results != null && results.hasMoreElements() ) {
    sortedResults.addElement((SearchResult)results.next());
    }
    results.close();
    return sortedResults;
    }

    Parsing those results is a whole other matter that you should be able to
    do yourself. Look at the javax.naming.directory.SearchResult class.

    Post again with more specific issues and I can help you more.
    Brandon McCombs, May 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Dees

    JNDI LDAP problem in JRE 1.4

    Ray Dees, Sep 5, 2003, in forum: Java
    Replies:
    0
    Views:
    721
    Ray Dees
    Sep 5, 2003
  2. Cram TeXeD
    Replies:
    0
    Views:
    747
    Cram TeXeD
    Feb 4, 2004
  3. Mark
    Replies:
    0
    Views:
    2,858
  4. Mike S
    Replies:
    0
    Views:
    22,982
    Mike S
    Sep 16, 2004
  5. Mick
    Replies:
    2
    Views:
    9,496
    Marcin Grunwald
    Feb 23, 2005
Loading...

Share This Page