LDAP Authentication
kmali Sep 9, 2009 11:06 AMHi I've implemented a simple LDAP authentication in a seam application.
Currenty only a logon name
and password authentication is possible.
I would like add that only users who are member of a certain group can pass the authentication.
How can I do this?
This my code:
import static org.jboss.seam.ScopeType.SESSION;
import java.util.Hashtable;
import java.util.List;
import javax.ejb.Stateless;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.security.Identity;
import javax.naming.*;
import javax.naming.*;
@Stateless
@Name("authenticator")
public class AuthenticatorAction implements Authenticator
{
@In
Identity identity;
public boolean authenticate()
{
Hashtable authEnv = new Hashtable(11);
String userName = identity.getUsername();
String passWord = identity.getPassword();
if (passWord=="")
return false;
String ldapURL = "ldap://f2.enterprise";
authEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
authEnv.put(Context.PROVIDER_URL, ldapURL);
authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
authEnv.put(Context.SECURITY_PRINCIPAL, userName+"@Research");
authEnv.put(Context.SECURITY_CREDENTIALS, passWord);
try {
DirContext authContext = new InitialDirContext(authEnv);
return true;
} catch (AuthenticationException authEx) {
return false;
} catch (NamingException namEx) {
return false;
}
}
} Thanks for your help.