Galera, criei uma tela de login para estudos, essa tela faz validação LDAP aqui na empresa.
Minha dúvida é, depois que o usuário e validado, como eu faço para abrir uma tela ?
Isso é, digite o usuário e senha, clica em entrar ai abrir uma tela.
Segue o codigo
Classe LDAP
public class LdapAuthentication {
public String INITIAL_CTX = "com.sun.jndi.ldap.LdapCtxFactory";
public String SERVIDOR = "ldap://0.0.0.0.0;
public String CONNECTION_TYPE = "simple";
public String ADMIN_DN = "MINERAL\\mmarques";
public String ADMIN_PW = "password-2012";
public String BASE_DN = "DC=mineral,DC=cetem";
public String MSG_ERROR_LDAP_CONNECTION = "Não foi possível obter um contexto LDAP";
public String MSG_ERROR_LDAP_VALIDATION_USER = "Username ou Password Inválida";
public String MSG_CONNECTION_SUCESS = "Conexão realizada com sucesso !!!";
public String MSG_AUTHENTICATION_USER = "Usuário autênticado com sucesso !!!";
/**
*
* @author mmarques
* @param login
* @param password
* @return
* Método para realizar autenticação do usuário, pega os valores pelo request.
*/
public boolean authentication(String login, String password) {
DirContext ctx = null;
SearchControls sc = null;
String filtro = null;
NamingEnumeration cursor = null;
boolean bResult = false;
ctx = createLdapConnection();
if (ctx != null) {
sc = new SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] atributosParaRetornar = { "distinguishedName" };
sc.setReturningAttributes(atributosParaRetornar);
filtro = "(&(sAMAccountName=" + login + "))";
try {
cursor = ctx.search(BASE_DN, filtro, sc);
if (cursor.hasMoreElements()) {
SearchResult result = (SearchResult) cursor.nextElement();
Attributes att = result.getAttributes();
String dn = (String) att.get("distinguishedName").get();
bResult = validateUser(dn, password);
}
} catch (NamingException e) {
System.out.println(MSG_ERROR_LDAP_CONNECTION);
e.printStackTrace();
}
}
return bResult;
}
/**
* Cria uma conexão no servidor LDAP
* @author mmarques
* @return
*/
private DirContext createLdapConnection() {
DirContext ctx = null;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CTX);
env.put(Context.PROVIDER_URL, SERVIDOR);
env.put(Context.SECURITY_PRINCIPAL, ADMIN_DN);
env.put(Context.SECURITY_CREDENTIALS, ADMIN_PW);
env.put(Context.SECURITY_AUTHENTICATION, CONNECTION_TYPE);
try {
ctx = new InitialDirContext(env);
System.out.println(MSG_CONNECTION_SUCESS);
}catch (NamingException e){
System.out.println(MSG_ERROR_LDAP_CONNECTION);
e.printStackTrace();
}
return ctx;
}
/**
*
* Valida se o usuário é verdadeiro
* @param dn
* @param senha
* @return
*/
private boolean validateUser(String dn, String senha) {
DirContext ldapCtx = null;
boolean bResult = false;
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CTX);
env.put(Context.PROVIDER_URL, SERVIDOR);
env.put(Context.SECURITY_PRINCIPAL, dn);
env.put(Context.SECURITY_CREDENTIALS, senha);
env.put(Context.SECURITY_AUTHENTICATION, CONNECTION_TYPE);
try {
ldapCtx = new InitialDirContext(env);
System.out.println(MSG_AUTHENTICATION_USER);
} catch (AuthenticationException auEx) {
System.out.println(MSG_ERROR_LDAP_VALIDATION_USER);
auEx.printStackTrace();
} catch (NamingException ne) {
System.out.println(MSG_ERROR_LDAP_CONNECTION);
ne.printStackTrace();
} finally {
if (ldapCtx != null) {
bResult = true;
}
}
return bResult;
}
}
CLASSE CONTROLE
public class LoginControler extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String password = request.getParameter("password");
LdapAuthentication ldap = new LdapAuthentication();
boolean valor = ldap.authentication(user, password);
if(valor == true){
System.out.println( user + " conectado com sucesso !!!");
}else{
System.out.println("Erro ao conectar !!!");
}
}
}
TELA LOGIN
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>..:: Login do usuário :..</title>
</head>
<body>
<br />
<br />
<br />
<form action="validarLogin" method="post">
<table align="center" height="100" width="300" border="1" >
<tr>
<td colspan="2" align="center"><img alt="cetem" src="Imagens\logo-cetem.png">
</tr>
<tr>
<td colspan="2" align="center" style="background-color: #2685b5; color: #ffffff;"><b>Login</b></td>
</tr>
<tr>
<td align="right">Usuário:</td>
<td><input type="text" align="left" name="user" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" align="left" name="password" /></td>
</tr>
<tr style="background-color: #2685b5" align="center">
<td colspan="2"><input type="submit" value="Entrar" ></td>
</tr>
</table>
</form>
</body>
</html>