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,
# 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);
}
}
*/
}
}