Posted By:
Aisling_Vasey
Posted On:
Tuesday, April 30, 2002 05:44 AM
Hi Kevin
I am also using WSAD but not really using a WebSphere specific code because I can run this example outside of WebSphere.
Here is my code for authenticating the UserName and password using an LDAP lookup:
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://yourServerName:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple"); //try and authenticate details
env.put(Context.SECURITY_PRINCIPAL, request.getParameter("username")); //non-blank UserName from login form
env.put(Context.SECURITY_CREDENTIALS, request.getParameter("password")); //non-blank Password from login form
//Attempt to connect to Server using the entered User details
try {
try {
DirContext ctx = new InitialDirContext(env);
//Look up all the available attributes
Attributes attr = ctx.getAttributes("uid="+request.getParameter("username")+",ou=mySubOrg,o=myOrg);
NamingEnumeration enum = attr.getAll();
while(enum.hasMoreElements())
{
Attribute at = (Attribute) enum.next();
System.out.println("Attribute:"+at.getID()+" "+at.get());
}
} catch (AuthenticationException ae) {
//Incorrect UserName and Password
request.setAttribute("loginMessage", "The UserName and Password are invalid. Enter your correct login details.");
getServletContext().getRequestDispatcher(loginPage).forward(request, response);
}
} catch (NamingException ne) {
//General LDAP error
request.setAttribute("exception", ne);
getServletContext().getRequestDispatcher(errorPage).forward(request, response);
}
I just loop through their attributes but I will try and do something useful with them, like security access restrictions when I get some time to figure it out.
Hope this helps.
(-: