faces-config
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
</locale-config>
</application>
<lifecycle>
<phase-listener>br.com.caelum.livraria.login.Autorizador</phase-listener>
</lifecycle>
</faces-config>
Autorizador.java
package br.com.caelum.livraria.login;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class Autorizador implements PhaseListener{
private static final long serialVersionUID = 1L;
public void afterPhase(PhaseEvent event) {
FacesContext context = event.getFacesContext();
if ("/login.xhtml".equals(context.getViewRoot().getViewId())) {
return;
}
ELContext elContext = context.getELContext();
ELResolver elResolver = context.getApplication().getELResolver();
UsuarioLogadoBean usuarioLogado = (UsuarioLogadoBean) elResolver.getValue(elContext, null, "usuarioLogadoBean");
// Usando o usuarioLogado que foi injetado
if (!usuarioLogado.isLogado()) {
NavigationHandler handler = context.getApplication()
.getNavigationHandler();
handler.handleNavigation(context, null, "login?faces-redirect=true");
//efetua renderizacao da tela
context.renderResponse();
}
}
@Override
public void beforePhase(PhaseEvent event) {
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}