How to Recover User Session

Asked

Viewed 2,362 times

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?

3 answers

5


The problem is how you recover the session. This should be done through Facescontext within the method in which you want to recover the session. It would look something like this:

@Transactional
public void gerarSimulado() {
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);
    Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
    // ...
}

You should remember that you cannot put two attributes in the session with the same name.

This name goes to the session here (user):

@ManagedBean(name = "usuarioLogado")

Trade for another or use Managedbean:

session.getAttribute("usuario");

3

Leonardo solved my problem. In the end the code was like this:

@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("identificaUsuario", usuario);

    }
    public Usuario getUsuario() {
        return usuario;
    }

}

And the method of the beginSimulated was as follows:

public void gerarSimulado() {

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        Usuario usuario = (Usuario)session.getAttribute("identificaUsuario"); 

 }

}

With the tip that Leonardo provided, it was possible to take the user’s session and cast Correctly on that line Usuario usuario = (Usuario)session.getAttribute("identificaUsuario"); besides changing the names of setAttribute and getattribute it was possible to get what I wanted, which was only the user data.

I hope I can help someone!

1

Well, in the stretch:

HttpSession session = (HttpSession) req.getSession();
Usuario usuario = (Usuario) session.getAttribute("nomeDoSeuBean");

You are trying to get the managedBean, not the attribute contained in it. For this, you should do the following:

HttpSession session = (HttpSession) req.getSession();
UsuarioSistema bean = (UsuarioSistema)  session.getAttribute("usuarioLogado");
Usuario usuario = bean.getUsuario();

Edit

Now that I realize that you are placing the attribute in the session and then trying to retrieve it in a request. Try to use the same scope as the stored attribute, in this case, in Sesssion.

  • Good morning, thank you for answering friend. The problem is that on this line. Httpsession Session = (Httpsession) req.getSession(); The req object is null, can you tell me why?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.