Skip to content

Instantly share code, notes, and snippets.

@edubriguenti
Last active January 1, 2016 23:49
Show Gist options
  • Save edubriguenti/8218946 to your computer and use it in GitHub Desktop.
Save edubriguenti/8218946 to your computer and use it in GitHub Desktop.
JSF redirectListener
public class RedirectLoginListener implements PhaseListener {
public static final LogManager LOGGER = LogManager.getLog(RedirectLoginListener.class);
private final Properties properties = PropertiesLoader.loadProperties(PropertiesConstants.PROPERTIES_PATH);
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
public void beforePhase(final PhaseEvent event) {
}
public void afterPhase(final PhaseEvent event) {
final DadosSessao dadosSessao = SessaoUtil.getInstance().getDadosSessao(FacesContextUtils.getRequest());
if (dadosSessao == null || dadosSessao.getDataValidade() == null) {
this.redirectToLoginPage();
} else {
final Calendar calendarCookie = dadosSessao.getDataValidade();
if (!calendarCookie.after(Calendar.getInstance())) {
this.redirectToLoginPage();
}
}
}
/**
* Método para redirecionar a sessão expirada para a página de login.
*/
private void redirectToLoginPage() {
try {
final String portalURL = "url";
SessaoUtil.getInstance().invalidarSessao(FacesContextUtils.getRequest(), FacesContextUtils.getResponse());
final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(portalURL);
} catch (final IOException e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment