JNDI and LDAP

xiaoxiao2021-03-06  41

Shang Yuzhen Zhongyuan University Electronic Engineering Department

Foreword

JNDI provides a hierarchical directory service interface: it has a category that can model simple naming services and simple directory service, and even have a Package dedicated to the LDAP directory. JNDI is a powerful tool for application program, and it can handle LDAP, ADS, etc., as the JDBC can handle Oracle, DB2. Using JNDI hides many complexity between the interfaces, however, today we will learn how JNDI is connecting and working with Directory Service through a paradigm.

JNDI Package Before starting our example, let's first understand what is the package it contains JAVA Naming and Directory Interface.

Javax.naming: javax.naming The core category and interface required to access the Java application access naming service. Javax.naming.directory: Javax.naming.directory is built on Javax.naming, which provides the required functionality when providing services provided by all LDAPs outside a small part of the services. Javax.naming.ldap: This package contains the categories needed to access the specific features of the LDAP directory service for the Java app and provide support for the LDAP Version 3 management function. These functions represent advanced LDAP features, which are not often used in most applications. Javax.naming.event: This Package contains the categories and interfaces required to support event notifications in the catalog service. Javax.naming.spi: Provides an architecture that establishes a basic JNDI class extension, allowing constructing other JNDI service providers. This package allows developers to have the ability to construct SUN's categories and directory services that have not supported SUN.

Tools and environments

IBM HTTP Server 1.3.19IBM Secureway Directory 3.2.2 JDK 1.3

The environment is ready to be able to perform the following program examples, first we will establish a profile in IBM Secureway Directory (ie Directory Service Server) for our next program example.

step one:

Enter Web Broswer and enter "http:// localhost / ldap /" in the URL to enter Secureway's Server Web Management interface, as shown below, and enter the manager's account, password (managed account is set (CN = root, The password is set in the installation process)

Step 2:

After entering the management interface, click Setting -> Siffixes and enter "O = IBM, C = US" in the SUFFIX DN field, and then click Update, and will be as shown in the following figure.

Step three:

Click Restart The Server in the screen, restart IBM Secureway Directory

Step 4:

We turn on the CLIENT interface via the start -> program -> IBM Secureway Directory -> Directory Management Tool, open the "rearrange" in the screen, select "Identification", enter the administrator's account password, and perform the login action

Step 5:

Click again to rearrange the directory tree (you will jump out of a warning window, please click "New", select "Organization" in the project type, the mother DN remains blank, the project RDN input "o = IBM C = US, then click OK Step Six:

Next, IBM Secureway Directory will jump out of a screen, ask you to enter other information of the entry, this section can not enter, click OK to add actions

Step 7:

Let's go under the entry we have just established, then build a person's information, first select the rearranged directory tree, then choose newly in? O = IBM, c = US? Users ", the mother DN remains unchanged (ie" o = IBM, c = us "), the project RDN input" CN = John ", then click OK

Step 8:

Like the same, IBM Secureway Directory will still jump out of the screen, ask you to enter the other information of the entry, then we choose "Smith" in the SN (last name) field, enter "User" in the UserPassword field (system will use an asterisk ),As shown below

Step Nine:

At this time, we have completed the actions of new information, and we can see the "cn = john" information under "o = IBM, C = US".

After the JNDI program is completed, then we will explain how JNDI is connected and operated by the Directory Service through a program example.

Import java.util.hashtable;

Import javax.naming.context;

Import javax.naming.directory.initialdirContext;

Import javax.naming.directory.dircontext;

Import javax.naming.directory.attribute;

Import javax.naming.namingexception;

Class Jndisample2

{

Public static void main (string [] args)

{

Try {

Hashtable env = new hashtable ();

Env.put (Context.Initial_Context_Factory, "com.sun.jndi.ldap.ldapctXFactory);

Env.put (Context.Provider_URL, "LDAP: // localhost: 389 /");

Dircontext CTX = New InitialDirContext (ENV);

Attributes attrs = ctx.getattributes ("cn = john, o = IBM, c = us");

System.out.println ("SN:" attrs.get ("sn"). Get ());

} catch (namingexception e) {

System.err.println ("Problem getting attribute: e);

}

}

}

The main purpose of this process is to take out the information we have just established in the SN field in this information, first we can see this code code.

Hashtable env = new hashtable (); env.put (context.initial_context_factory, "com.sun.jndi.ldap.ldapctxfactory");

Env.put (Context.Provider_URL, "LDAP: // localhost: 389 /");

Once the HashTable environment is properly constructed, we can case a new DirContext so that you can create a LDAP service to use the Java application.

Dircontext CTX = New InitialDirContext (ENV);

Then we specify that we want to search for DNs.

Attributes attrs = ctx.getattributes ("cn = john, o = IBM, c = us");

And specify that we want to take the name of the field, use JNDI's Java API, we can request from a Directory Service to ask us to request.

System.out.println ("SN:" attrs.get ("sn"). Get ());

After the use of JNDI, with the usually we feel very similar to the Java SQL API, we must first specify the manufacturer Driver, then specify the link position, then give a given condition, finally take out what we need, the power of the Directory Service in this example It is not obvious that we can learn from JNDI standards, we can learn from the use of Directory Service.

转载请注明原文地址:https://www.9cbs.com/read-60906.html

New Post(0)