4
I just wanted to retrieve a user’s session. I have a class called User system, that identifies which user is logging in. In this class, I take the session and play for an object.
@ManagedBean(name = "usuarioLogado")
@SessionScoped
public class UsuarioSistema extends User implements Serializable {
private static final long serialVersionUID = 1L;
private Usuario usuario;
public UsuarioSistema(Usuario usuario, Collection<? extends GrantedAuthority> authorities) {
super(usuario.getEmail(), usuario.getSenha(), authorities);
this.usuario = usuario;
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
session.setAttribute("usuarioLogado", usuario);
}
public Usuario getUsuario() {
return usuario;
}
}
I try to recover this session when the user generates a simulated, ie in a bean. However, it always comes null.
@Named
@viewScoped
public class GerarSimuladoBean implements Serializable {
private static final long serialVersionUID = 1L;
HttpServletRequest req = (HttpServletRequest);
@Transactional
public void gerarSimulado() {
HttpSession session = (HttpSession) req.getSession();
Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
//......
}
Can anyone tell me what I’m doing wrong, and why I can’t recover the user?
It sure will help.
– Leonardo Costa