Boa madrugada drsmachado,
Eu ainda aqui tentando. Hehehe.
Bom vamos lá, a seu conselho fui pesquisar um pouco de JAAS..
Gostei muito e estou quase terminando, porem ( à sempre um porem né ), estou com um probleminha..
Se eu digitar a url do index.xhtml vai para tela de login.. até ai maravilha era oque eu queria, eu estou digitando o login e a senha correto e não esta indo para o index..
Lembrando que estou usando JBoss EAP 6.1
tenho um banco chamado teste nele tenho uma tabela chamada user e tem as seguintes colunas:
userID: chave primario
userName
password
permissao
Bom vamos ao meu context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/teste"
connectionName="root" connectionPassword="root"
userTable="user" userNameCol="userName"
userCredCol="password" userRoleTable="user"
roleNameCol="permissao"/>
</Context>
Meu web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>TesteUsuário</display-name>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/login.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>Consulta Estatistica</web-resource-name>
<url-pattern>/index.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
</web-app>
Meu login.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>TesteUsuario</title>
<link type="text/css" rel="stylesheet"
href="#{facesContext.externalContext.requestContextPath}/css/template.css" />
</h:head>
<h:body>
<h:form prependId="false">
<p:spacer height="250px" />
<p:panel header="Bem-Vindo(a)"
style="width:500px;margin-left:auto;margin-right:auto;
border: 1px solid #000000;box-shadow:10px 10px 5px black;">
<p:messages id="messages" showDetail="true" autoUpdate="true"
closable="true" />
<h:panelGrid columns="2" style="margin-left:auto;margin-right:auto;">
<h:outputLabel value="Usuário:" />
<p:inputText value="#{login.login}" />
<h:outputLabel value="Senha:" />
<p:password value="#{login.password}" />
</h:panelGrid>
<h:panelGrid columns="2" style="margin-left:auto;margin-right:auto;">
<p:commandButton type="submit" id="login" value="Entrar"
action="#{login.logar}" ajax="false" />
<p:commandButton id="clear" value="Limpar Campos" action="erro"
ajax="false" />
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
E por fim meu Bean:
import javax.faces.context.FacesContext;
import javax.faces.bean.ManagedBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ManagedBean
public class Login {
private String login;
private String password;
public String logar() {
try {
this.getRequest().login(this.login, this.password);
return "index?faces-redirect=true";
} catch (ServletException e) {
return null;
}
}
public String sair() throws ServletException {
this.getRequest().logout();
return "Login?faces-redirect=true";
}
private HttpServletRequest getRequest() {
FacesContext context = FacesContext.getCurrentInstance();
return (HttpServletRequest) context.getExternalContext().getRequest();
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Agradeço desde já sua paciência.