how to access LDAP?

Hi,

I have a requirement based on following constraints:-

1. All users should be authenticated using LDAP username and password.

2. If they are authenticated a session must be created for them and it should be maintained.

Please tell me how to do it in detail as I am a newbie to programming.

Thanks,

[337 byte] By [PaviEluri20a] at [2007-11-27 8:40:24]
# 1
Is Google not installed on your machine?Just because you're a newbie to programming doesn't excuse from doing the work. Go and search the web, read the articles, try things out, and then come back with any specific problems you may encounter.
evnafetsa at 2007-7-12 20:38:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Dear evnafets,"Time is money". Thanks for your advice though.
PaviEluri20a at 2007-7-12 20:38:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

And how much money are you paying for his time?

Most people here are happy to help with specific problems when you've demonstrated an effort in trying to solve the problem yourself.

Most people are not happy to help when somebody acts like help is due to them by virtue of merely asking.

Maybe you'll get lucky and somebody will decide to do your work for you. Or maybe not.

es5f2000a at 2007-7-12 20:38:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

If I were a registered member of sun forum since '03 or '04 I would have probably avoided either replying to this simple question or might have answered it to the maximum extent.

Time makes a lot of difference my friends. Code or article read from online may or may not work some times which doesn't mean that the author is dumb or has no work done before posting this silly question.

Thanks.

PaviEluri20a at 2007-7-12 20:38:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Here is the skelton code to do it. I got the .class file. However, this code is not standard and I recommed to modify it according to u r use.

// This is the code to connect to the Ldap directory

// By changing the required port numbers and specifying the URL for ldap

// it is possible to connect to the Directory.

// @ Author Mr. India

//

import javax.naming.*;

import javax.naming.directory.*;

import java.util.Hashtable;

import java.util.StringTokenizer;

public class LdapQuery {

public static void main(String[] args) {

//Check that there's exactly one command line argument.

//if (args.length != 1) {

// System.out.println("Syntax: LdapQuery query");

// return;

//}

// Read the command line argument and confirm to the user.

//String query = args[0];

//System.out.println();

//System.out.println("Running query: " + query);

//System.out.println();

//Initialize the factory

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

// Provide the URL of the factory

env.put(Context.PROVIDER_URL, "ldap://apjkalam.com:389/");

/* try {

// To see all data in the Bath LDAP, use directory root o=apjkalam.com

String dirRoot = "o=apjkalam.com";

//Specify the ldap directory of apjkalam here

// Make a directory context by connecting with the above details.

DirContext context = new InitialDirContext(env);

// Set up how we want to search - in this case the entire subtree

// under the specified directory root.

SearchControls ctrl = new SearchControls();

ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Now do the search, using the specified query and search controls.

NamingEnumeration<SearchResult> enumeration = context.search("", query, ctrl);

// We get back an enumeration which may contain any number of results,

// so work through them one at a time.

while (enumeration.hasMore()) {

System.out.println("========================================================");

SearchResult result = enumeration.next();

// The Distinguished Name is the primary key for an LDAP entry.

String dn = result.getName() + "," + dirRoot;

System.out.println("DN : " + dn);

System.out.println("--");

// Extract the attributes (if any) from the current entry.

Attributes attribs = result.getAttributes();

// Simplest possible way to display an attribute's value(s)...

System.out.println(((BasicAttribute) attribs.get("employeeType")).toString());

// A more normal way to get at attribute values.

// NB: Most attributes are Strings, but there are one or two that

// will give an exception if you try and cast them to a String.

// NB2: This only gets the first value. If there are several then

// cnAttrib.get(1), etc, will return them.

BasicAttribute cnAttrib = (BasicAttribute) attribs.get("cn");

String commonName = (String) cnAttrib.get(0);

System.out.println("Known As: " + commonName);

}

} catch (NamingException ne) {

System.out.println("Error: " + ne);

}

}

*/

}

}

PaviEluri20a at 2007-7-12 20:38:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Most application servers come with the ability to authenticate against LDAP. For Tomcat it is under JNDI realm: http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.htmlCheck the documentation for your server
tolmanka at 2007-7-12 20:38:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...