JSF Java - Não consigo obter dados do usuário Logado

1 resposta Resolvido
brunoo

Ola galera!

Não está sendo possível recuperar os dados do usuário logado e gostaria de ajuda pois já tentei 3 formas diferentes de implementação.

@ManagedBean(name = "loginController")
public class LoginBean extends GrowlView implements Serializable {

	private static final long serialVersionUID = 1L;
	private String email;
	private String password;
	public static Integer idUsuarioLogado;
	private MenuModel model;
	private Login investidorLogin;

public String logarNoSistema() throws SQLException {
Se o usuario estiver cadastrado, então:

investidorLogin = new Login();
			investidorLogin.setInvalidInformation(null);
			investidorLogin.setLogin(resultSet.getString("email"));
			investidorLogin.setEmail(resultSet.getString("email"));
			investidorLogin.setSenha(resultSet.getString("senha"));

FacesContext fc = FacesContext.getCurrentInstance();
			HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
			session.setAttribute("login", investidorLogin);

return "/restricted/userpanel?faces-redirect=true";

Página de login de teste:

<h:form id="formLogin">
		<h:outputLabel value="email" />
		<h:inputText id="login" styleClass="teste"
			value="#{loginController.email}" required="true"
			placeholder="Email" type="email"
			requiredMessage="Login has not been typed" />
		<br />
		<h:outputLabel value="senha" />
		<h:inputText id="Senha" styleClass="teste"
			value="#{loginController.password}" required="true"
			placeholder="Email" type="email"
			requiredMessage="Login has not been typed" />
		<p:commandButton styleClass="submit fb-btn" value="Login"
			action="#{loginController.logarNoSistema}" icon="pi pi-check"
			update="formLogin" widgetVar="saveButton"
			onclick="PF('saveButton').disable()"
			oncomplete="PF('saveButton').enable()" />
	</h:form>

Segue a pagina de teste após login que criei para coletar os dados do usuario logado. Os dados estão vindo vazios:

<h:body>
		<h1>Usuario Logado</h1>
		<h1>#{loginController.investidorLogin.email}</h1>
		<h1>#{loginController.email}</h1> 		
		<h:outputLabel value="#{loginController.email}" />
		<h:outputLabel value="#{loginController.investidorLogin.email}" />
</h:body>

Classe Filter:

@WebFilter("/seguranca/login.xhtml")
public class LoginFilter implements Filter {

	public LoginFilter() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = (HttpSession) req.getSession();

		Login investidorLogin = (Login) session.getAttribute("login");
		
		if (investidorLogin == null) {
			chain.doFilter(request, response);
		} else {			
			res.sendRedirect(req.getContextPath() + "/restricted/index3.xhtml");
		}
		
	}

1 Resposta

brunoo
Solucao aceita

Pessoal, descobri o problema.

Isso aconteceu porque a classe LoginBean estava com ‘extends’. Removi o ‘extends’ e funcionou.

Criado 12 de abril de 2020
Ultima resposta 12 de abr. de 2020
Respostas 1
Participantes 1