Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

login não redireciona corretamente

Fiz um projeto análogo ao do curso, como forma de fixar meu aprendizado,.

Neste projeto, a entidade livro foi trocada por veiculo, e Autor por proprietário. No entanto, ao fazer login, o console cospe a saída : chamando Login

FASE: RESTORE_VIEW 1
/veiculo.xhtml
FASE: RESTORE_VIEW 1
/login.xhtml
FASE: RENDER_RESPONSE 6

ele chama a página veiculo, mas depois renderiza para login novamente. Meu método de Login em LoginBean

public String efetuaLogin() {
        System.out.println("entreo");
        boolean existe = new UsuarioDAO().existe(this.usuario);
        System.out.println("chamando Login");
        FacesContext context = FacesContext.getCurrentInstance();

        if (existe) {
            System.out.println(usuario.getEmail() + " " + usuario.getSenha());
            context.getExternalContext().getSessionMap().put("usuariologado", this.usuario);
            return "veiculo?faces-redirect=true";
        }
    context.getExternalContext().getFlash().setKeepMessages(true);
        context.addMessage(null, new FacesMessage("Usuario não encontrado"));
        return "login?faces-redirect";
    }

Minha classe Autorizador

package br.com.veiculoimposto.util;

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;
import br.com.veiculoimposto.modelo.Usuario;

public class Autorizador implements PhaseListener {

    private static final long serialVersionUID = 1L;
    @Override
    public void afterPhase(PhaseEvent evento) {

        FacesContext context = evento.getFacesContext();
        String nomePagina = context.getViewRoot().getViewId();

        System.out.println(nomePagina);

        if("/login.xhtml".equals(nomePagina)) {
            return;
        }

        Usuario usuarioLogado = (Usuario) context.getExternalContext().getSessionMap().get("usuarioLogado");

        if(usuarioLogado != null) {
            return;
        }
        //redirecionamento para login.xhtml    
        NavigationHandler handler = context.getApplication().getNavigationHandler();
        handler.handleNavigation(context, null, "/login?faces-redirect=true");
        context.renderResponse();
    } 
    @Override
    public void beforePhase(PhaseEvent event) {
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }

}

Alguem tem alguma ideia do que pode está acontecendo ?

3 respostas

A saída do console é :

fev 27, 2018 9:22:55 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
fev 27, 2018 9:22:55 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
fev 27, 2018 9:22:55 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
fev 27, 2018 9:22:55 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
fev 27, 2018 9:22:55 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
fev 27, 2018 9:22:55 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
fev 27, 2018 9:22:55 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
fev 27, 2018 9:22:55 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/impostoveiculodb]
fev 27, 2018 9:22:55 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****, autocommit=true, release_mode=auto}
fev 27, 2018 9:22:55 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
fev 27, 2018 9:22:55 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
fev 27, 2018 9:22:55 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: impostoveiculodb.DAF
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [proprietario_id, sequencia, valor, id]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk107694adde09b]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fk107694adde09b, primary]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: impostoveiculodb.Proprietario
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [numeroinscricaofiscal, nome, id]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: impostoveiculodb.Usuario
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [senha, id, email]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: impostoveiculodb.Veiculo
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [preco, proprietario_id, tipo, datadefabricacao, id, placa]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk77a7c7ef4adde09b]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fk77a7c7ef4adde09b, primary]
fev 27, 2018 9:22:56 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select usuario0_.id as id0_, usuario0_.email as email0_, usuario0_.senha as senha0_ from Usuario usuario0_ where usuario0_.email=? and usuario0_.senha=? limit ?
chamando Login
FASE: RESTORE_VIEW 1
/veiculo.xhtml
FASE: RESTORE_VIEW 1
/login.xhtml
FASE: RENDER_RESPONSE 6
solução!

Opa, você faz o put com a string "usuariologado", mas verifica se o usuário está na sessão com string "usuarioLogado". O L ta da primeira ta minúsculo.

era realmente isso !

Obrigado

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software