package com.ecom.data.access.controller;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.opends.server.admin.client.ldap.LDAPConnection;
import org.opends.server.types.ResultCode;
import org.springframework.ldap.AuthenticationException;
import org.springframework.ldap.NamingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.unboundid.ldap.sdk.BindRequest;
import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.SimpleBindRequest;
public class LoginController implements Controller
{
public static String usersContainer = "dc=example,dc=com";
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception
{
String givenName1=request.getParameter("name");
String userpassword1=request.getParameter("password");
System.out.println("Ldap wellcome");
final SimpleBindRequest bindRequest=new SimpleBindRequest(givenName1, userpassword1);
System.out.println("before");
System.out.println(bindRequest);
Hashtable<String, Object> env = new Hashtable<String, Object>(11);// Here we set some connection Hashtable for JNDI
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
env.put( Context.SECURITY_PRINCIPAL, "cn=Directory Manager" );
env.put( Context.SECURITY_CREDENTIALS, "admin" );
DirContext ctx = null;
NamingEnumeration<?> results = null;
try {
ctx = new InitialDirContext(env);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String lookup="givenName="+givenName1+",dc=example,dc=com";
String obj = "(objectclass=*)";
results=ctx.search(lookup, obj, controls);
while (results.hasMore())
{
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attruser = attributes.get("givenName");
Attribute attrpwd=attributes.get("uid");
Attribute org=attributes.get("o");
String cn = (String)attruser.get();
String cn1 = (String)attrpwd.get();
String cn2 = (String)org.get();
List<String> li = new ArrayList<String>();
li.add(cn);
li.add(cn1);
li.add(cn2);
if(givenName1.equals(cn) && userpassword1.equals(cn1))
{
ModelAndView modelSuccess=new ModelAndView("loginPage");
modelSuccess.addObject("msgSuccess", li);
return modelSuccess;
}
else
{
ModelAndView modelError=new ModelAndView("errorPage");
modelError.addObject("msgError", "Invalid UserName and Password");
return modelError;
}
}
} catch (Throwable e)
{
} finally
{
if (results != null)
{
try
{
results.close();
} catch (Exception e)
{
}
}
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
}
}
}
return null;
}
}
I am using Spring 3. I am working on LDAP. I need to do a user is authenticate if his user name and password is correct. I have many fields in my OpenDs. If I am using given name and first name then it will authenticate, but if I use password then it's not authenticated because the password stored in openDs is encrypted form. So I need to match the password. Please give me some suggestion for this. I am using the code as above.