Bom dia pessoal,
Estou tentando fazer um sistema de login utilizando o PhaseListener. O problema é que o PhaseListener não deixa o bean do login executar.
Segue o código:
PhaseListener
public class AuthorizationListener implements PhaseListener {
public void afterPhase(PhaseEvent event) {
FacesContext facesContext = event.getFacesContext();
String currentPage = facesContext.getViewRoot().getViewId();
boolean isLoginPage = (currentPage.lastIndexOf("Login.jsf") > -1);
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
Usuario logado = (Usuario)session.getAttribute("logado");
if (!isLoginPage && logado == null) {
NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
nh.handleNavigation(facesContext, null, "Login");
}
}
public void beforePhase(PhaseEvent event) {
}
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}
Login.jsf
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view>
<h:form>
<h:outputText value="teste" />
<h:inputText value="#{Usuario.nome}" />
<h:commandButton action="#{Usuario.verifica}" value="Verifica" />
<h:outputText value="#{Usuario.nome}" />
</h:form>
<h:messages />
</f:view>
</html>
Principal.jsf
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:rich="http://richfaces.org/rich">
<f:view>
<rich:calendar>
</rich:calendar>
<h:inputText value="#{Usuario.nome}" />
</f:view>
</html>
Usuario.java
package Controle;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import bsh.This;
public class Usuario {
public String nome = "";
FacesContext facesContext = FacesContext.getCurrentInstance();
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String verifica() throws CloneNotSupportedException{
if(nome.equals("X")){
Usuario usuario = new Usuario();
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) facesContext.getCurrentInstance().getExternalContext().getRequest();
HttpSession session = request.getSession();
session.setAttribute("logado", usuario);
return "Principal";
}else{
return "Login";
}
}
}
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
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-facesconfig_1_2.xsd">
<managed-bean>
<managed-bean-name>Usuario</managed-bean-name>
<managed-bean-class>Controle.Usuario</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>Principal</from-outcome>
<to-view-id>/Principal.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>Login</from-outcome>
<to-view-id>/Login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
<lifecycle>
<phase-listener>Controle.AuthorizationListener</phase-listener>
</lifecycle>
</faces-config>