Redirect to another page only if the user is authenticated

5

I have a login screen where I authenticate a user, however when I put a action to change the page independently whether the user was authenticated or not the page is opened.

My question is to know where I have to check whether the user is authenticated or not to redirect?

Button:

<f:facet name="footer">
        <p:commandButton value="Entrar" update=":msgGlobal"
            actionListener="#{usuarioBean.entrar}" action="principal?faces-redirect=true"/>
</f:facet>

Authentication method:

public void entrar() {
    try {
        UsuarioDAO usuarioDAO = new UsuarioDAO();
        usuarioLogado = usuarioDAO.autenticar(usuarioLogado.getLogin(),
                    usuarioLogado.getSenha());
        if (usuarioLogado == null) {
                FacesUtil.adicionarMsgErro("Login ou Senha inválidos");
        } else {
                FacesUtil.adicionarMsgInfo("Usuario Autenticado com Sucesso");
        }
    } catch (RuntimeException ex) {
        FacesUtil.adicionarMsgErro("Erro ao tentar entrar no Sistema");
    }
}
    
asked by anonymous 10.06.2015 / 18:54

1 answer

3
public void entrar() {
    try {
        UsuarioDAO usuarioDAO = new UsuarioDAO();
        usuarioLogado = usuarioDAO.autenticar(usuarioLogado.getLogin(),    usuarioLogado.getSenha());
        if (usuarioLogado == null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect(/* url que vc quer*/);
        } else {
            FacesUtil.adicionarMsgInfo("Usuario Autenticado com Sucesso");
        }
    } catch (RuntimeException ex) {
        FacesContext.getCurrentInstance().getExternalContext().redirect(/* url que vc quer*/);
    }
}
    
10.06.2015 / 19:20