Created
November 4, 2014 16:12
-
-
Save dilnei/2dfd28353391a9117189 to your computer and use it in GitHub Desktop.
LoginPhaseListener para JSF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.weblog.listener; | |
import br.com.weblog.view.UsuarioLogado; | |
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; | |
/** | |
* Phase Listener - possui o mesmo comportamento de um filtro porém utilizando | |
* Phase Listener podemos interceptar cada uma das diferentes fases do JSF e | |
* executar algo antes ou depois de cada uma delas. | |
* | |
* @author Dilnei Cunha | |
*/ | |
public class LoginPhaseListener implements PhaseListener { | |
/** | |
* Estamos validando se o usuário está logado, podemos escolher em que | |
* momento podemos fazer a validação, neste caso vamos fazê-la depois da | |
* Phase que definimos no método getPhaseId. | |
* | |
* @param event | |
*/ | |
@Override | |
public void afterPhase(PhaseEvent event) { | |
FacesContext context = event.getFacesContext(); | |
if ("/index.xhtml".equals(context.getViewRoot().getViewId())) { | |
return; | |
} | |
UsuarioLogado usuarioLogado = context.getApplication().evaluateExpressionGet(context, | |
"#{usuarioLogado}", UsuarioLogado.class); | |
if (!usuarioLogado.isLogado()) { | |
NavigationHandler handler = context.getApplication().getNavigationHandler(); | |
handler.handleNavigation(context, null, "index"); | |
context.renderResponse(); | |
} | |
} | |
@Override | |
public void beforePhase(PhaseEvent event) { | |
} | |
/** | |
* Definimos em qual fase do JSF sera feito a verificação se o usuário está | |
* logado | |
* | |
* @return | |
*/ | |
@Override | |
public PhaseId getPhaseId() { | |
return PhaseId.RESTORE_VIEW; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment