EDIT: El titulo debería ser
"Metodo para validar usuario en OpenLDAP", no puedo modificar el titulo, si un MOD lo puede hacer se lo agradezco
Estoy trabajando en un metodo para validar usuarios contra un OpenLDAP, se que se puede hacer por medio de "container validation2 pero realmente no quiero depender del contenedor, quiero hacer algo que funcione para Tomcat, Wildfly e incluso Glassfish.
Por ahora logre hacer un metodo y el logeo me da que se conecta bien al OpenLDAP, le puse 3 salidas a terminal solo para ver si lo hace bien, una para cuando el user esta bien pero el password no, otra cuando el user esta mal y otra cuando ambos estan bien.
Siempre me sale el mismo error (el de user OK y pass mal).
Estuve haciendo debugging y al parecer el error esta en el string que hace de la consulta, pero no estoy pudiendo dar con el clavo para corregirlo
El metodo
Código Java:
Ver original Hashtable
<String, String
> env
= new Hashtable
<String, String
>();
String LDAP_SERVER
= "127.0.0.1"; String LDAP_SERVER_PORT
= "389"; String LDAP_BASE_DN
= "dc=domain,dc=moredata,dc=com"; String LDAP_BIND_DN
="cn=user,dc=moredata,dc=com"; String LDAP_BIND_PASSWORD
="mypassword";
env.
put(Context.
INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory"); env.
put(Context.
PROVIDER_URL,
"ldap://" + LDAP_SERVER
+ ":" + LDAP_SERVER_PORT
+ "/" + LDAP_BASE_DN
);
// To get rid of the PartialResultException when using Active Directory
env.
put(Context.
REFERRAL,
"follow");
// Needed for the Bind (User Authorized to Query the LDAP server)
env.
put(Context.
SECURITY_AUTHENTICATION,
"simple"); env.
put(Context.
SECURITY_PRINCIPAL, LDAP_BIND_DN
); env.
put(Context.
SECURITY_CREDENTIALS, LDAP_BIND_PASSWORD
);
try {
}
NamingEnumeration<SearchResult> results = null;
try {
controls.
setSearchScope(SearchControls.
SUBTREE_SCOPE); // Search Entire Subtree controls.setCountLimit(1); //Sets the maximum number of entries to be returned as a result of the search
controls.setTimeLimit(5000); // Sets the time limit of these SearchControls in milliseconds
String searchString
= "(&(objectCategory=users)(sAMAccountName=" + userName
+ "))";
results = ctx.search("", searchString, controls);
if (results.hasMore()) {
Attribute dnAttr
= attrs.
get("distinguishedName");
// User Exists, Validate the Password
env.
put(Context.
SECURITY_PRINCIPAL, dn
); env.
put(Context.
SECURITY_CREDENTIALS, userPassword
);
//show validation suceed
System.
out.
println("Validation suceed"); return true;
}
else
//User exist but password is wrong
System.
out.
println("User OK, pass no"); return false;
//Tiro en consola el error
System.
out.
println("autentication error"); return false;
return false;
throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e
); } finally {
if (results != null) {
try { results.
close(); } catch (Exception e
) { /* Do Nothing */ } }
if (ctx != null) {
try { ctx.
close(); } catch (Exception e
) { /* Do Nothing */ } }
}
}
El formulario
Código HTML:
Ver original<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
</h:head>
<h:body>
<center>
<h:form id="Login" style="max-width: 50%; border: solid 1px; margin-bottom: 15px">
<p:growl />
<p:panelGrid columns="2" style="margin-top: 15px">
<h:outputText value="Nombre" />
<h:inputText id="nombre" value="#{authBean.userName}" required="true"/>
<h:outputText value="Password" />
<h:inputSecret id="password" value="#{authBean.userPassword}" required="true"/>
</p:panelGrid>
<p:commandButton ajax="false" process="@all" update="@all" action="#{authBean.validateLogin(authBean.userName, authBean.userPassword)}" value="Login" />
</h:form>
</center>
</h:body>