Help with JSF login menu in JavaWeb

1

Talk, people, beauty? Hi, I am trying to create a Java project with Hibernate, TDD, MVC, JSF, Primefaces and more that I can not remember. Next, when I log in to my project I make the menu appear but when I click on a function, it disappears. The authentication test is working ok but it's as if the user were to close and lose the login.

By logging in, you will see:

WhenIclickonanyfunction,itwilladd:

TheMenuisdonelikethisintheJSF:

<p:submenulabel="Usuários" rendered="#{autenticacaoBean.usuarioLogado.funcao != null}">
        <p:menuitem value="Pesquisa e Listagem" outcome="/pages/usuarioPesquisa.xhtml" icon="ui-icon-search" />
        <p:menuitem value="Criar" outcome="/pages/usuarioCadastro.xhtml" icon="ui-icon-disk">
             <f:param name="usuAcao" value="criar" />
        </p:menuitem>
    </p:submenu>

The login part looks like this:

<h:form>

    <h:panelGrid columns="2">

    <p:outputLabel value="Email"></p:outputLabel>
    <p:inputText size="30" value="#{autenticacaoBean.usuarioLogado.email}">
    <f:validateBean />
    </p:inputText>

    <p:outputLabel value="Senha"></p:outputLabel>
    <p:password size="30" value="#{autenticacaoBean.usuarioLogado.senha}">
    <f:validateBean />
    </p:password>

    </h:panelGrid>

    <h:panelGrid>

    <p:commandButton value="Entrar" update=":msgGlobal :formLogin:painelMenu"
    actionListener="#{autenticacaoBean.autenticar}"></p:commandButton>

    </h:panelGrid></h:form> 

Bean Authentication looks like this:

public class AutenticacaoBean {

private Usuario usuarioLogado;

public Usuario getUsuarioLogado() {
    if(usuarioLogado == null){
        usuarioLogado = new Usuario();
    }
    return usuarioLogado;
}

public void setUsuarioLogado(Usuario usuarioLogado) {
    this.usuarioLogado = usuarioLogado;
}

public void autenticar(){
    try {
        UsuarioDAO usuariodao = new UsuarioDAO();
        usuarioLogado = usuariodao.autenticar(usuarioLogado.getEmail(), DigestUtils.md5Hex(usuarioLogado.getSenha()));

        if(usuarioLogado == null){
            FacesUtil.addMsgErro("Email ou Senha inválidos !");
        } else {
            FacesUtil.addMsgInfo("Usuário Autenticado com sucesso!");
        }

    } catch(RuntimeException ex) {

        FacesUtil.addMsgErro("Erro ao tentar autenticar no sistema.");
    }
}}

and in the UserID is as follows:

public Usuario autenticar(String email, String senha){
    Session sessao = HibernateUtil.getSessionFactory().openSession();
    Usuario usuario = null; 

    try {
        Query consulta = sessao.getNamedQuery("Usuario.autenticar");
        consulta.setString("email", email);
        consulta.setString("senha", senha);
        usuario = (Usuario) consulta.uniqueResult();
    } catch (RuntimeException ex) {
        throw ex;
    } finally {
        sessao.close();
    }
    return usuario;
}
    
asked by anonymous 26.09.2016 / 20:28

1 answer

1

Is AuthenticationBean with Session Scope? try adding the @SessionScoped annotation to the Bean Authentication.

    
26.09.2016 / 21:26