How to recover user session

3

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?

    
asked by anonymous 26.10.2015 / 02:08

3 answers

5

The problem is how you recover the session. This should be done through the FacesContext within the method where 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 can not put two attributes in the session with the same name.

This name goes to the session here (userLogin):

@ManagedBean(name = "usuarioLogado")

Switch to another or use ManagedBean:

session.getAttribute("usuario");
    
26.10.2015 / 14:00
3

Leonardo solved my problem. At the end of the day the code looks 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 generateSimulate 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 get the user session and casting correctly on this line Usuario usuario = (Usuario)session.getAttribute("identificaUsuario"); besides changing the names of setAttribute and getAttribute it was possible to get what I wanted, it was just the data of user.

Hope you can help someone!

    
26.10.2015 / 14:36
1

Well, in the excerpt:

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 putting the attribute in the session and then try to retrieve it in a request. Try to use the same scope as the attribute was stored, in this case, in the session.

    
26.10.2015 / 13:17