I wanted to just retrieve a user's session. I have a class called SystemUser , which identifies which user is logging on. In this class, I get 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 retrieve this session at the time the user generates a dummy, that is, 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 not recover the user?