Here the variables strUsername and strPassword are entered on the login html page which are passed to JSP and then to JNDI-LDAP code given below.
I replaced the above two variables with a specific information which I had created in the directory of the Server , the code then works.
import java.util.Hashtable;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.naming.directory.*;
public class JNDISearch
{
// initial context implementation
public static final
String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static final String MY_HOST = "ldap://wnctg53.yogesh.com:389";
public static String MY_SEARCHBASE = "ou=People,o=yogesh.com";
//specify which attributes we are looking for
public boolean String MY_ATTRS[] = {"uid"};
/*public static void main(String args[])
{
JNDISearch obj = new JNDISearch();
obj.method1();
}*/
public String method1(strUserName, strPassword)
{
boolean flag = true;
String MGR_PW = strPassword;
String MGR_DN = "uid="+strUserName+",ou=People,o=yogesh.com";
String MY_FILTER = "(uid="+strUserName+")";
try
{
//Hashtable for environmental information
Hashtable env = new Hashtable();
//Specify which class to use for our JNDI provider
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
// Specify host and port to use for directory service
env.put(Context.PROVIDER_URL, MY_HOST);
// Setting the securit level
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// Verifying the username
env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
// Verifying the password
env.put(Context.SECURITY_CREDENTIALS, MGR_PW);
//return flag;
//System.out.println("Hi, you are in method1");
//now step through the search results
//Get a reference to a directory context
DirContext ctx = new InitialDirContext(env);
//specify the scope of the search
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//perform the actual search
//we give it a searchbase, a filter and constraints
//containing the scope of the search
NamingEnumeration results =
ctx.search(MY_SEARCHBASE, MY_FILTER,constraints);
//now step through the search results
while (results != null && results.hasMore())
{
SearchResult sr = (SearchResult) results.next();
String dn = sr.getName() + "," + MY_SEARCHBASE;
//System.out.println("Distinguished Name is "+dn);
Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
if (ar == null)
{
flag = false;
}
else {
for (int i =0;i<MY_ATTRS.length;i++)
{
Attribute attr = ar.get(MY_ATTRS[i]);
String check = (String)attr.get();
if ( check.equalsIgnoreCase(strUserName))
{
flag = true;
}
else
{
flag = false;
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("**** The flag : " + flag);
return flag;
}
}