How to create a session scoped bean?

863 Views Asked by At

I would like to know how to crete a user session object that can be used across all user requests. The user can select profiles after login, so I need to update that session object with this profile data.

How is it done? How do I initialize a session scoped bean? How can I change objects in that SessionScoped bean later (it is necessary to remember some user actions in his session).

I would be very happy if you could help me in that matter :)

@SessionScoped @Named
public class UserSession implements Serializable {

    private ExtendedUserPrincipal extendedUserPrincipal;

    @PostConstruct
    private void instantiateSession() {
        extendedUserPrincipal = new ExtendedUserPrincipal();
    }

    public void setUserPrincipal(UserPrincipal userPrincipal) {
        extendedUserPrincipal.setUserPrincipal(userPrincipal);
    }

    public void setUser(User user) {
        extendedUserPrincipal.setUser(user);
    }

    public void setUserSecurityData(UserSecurityData userSecurityData) {
        extendedUserPrincipal.setUserSecurityData(userSecurityData);
    }

    @Produces @AuthenticatedUser
    private ExtendedUserPrincipal getPrincipal() {
        return extendedUserPrincipal;
    }

}

I invalidate the session by calling logout() and invalidate() on the session which I get from HttpServletRequest.

I'm injecting the user principal like this. The object should be the same for every user session

@Inject
@AuthenticatedUser
private ExtendedUserPrincipal extendedUserPrincipal;
1

There are 1 best solutions below

1
On

I'm not sure what all the annotations mean, but you should just be able to put objects in the session manually with your HttpServletRequest object.

request.getSession().setAttribute("userbean", yourBean);

Then if you need to update it just get and set it like any other map

request.getSession().getAttribute("userbean");

The userbean will remain in the session until it is invalidated.


There are many different java libraries with different @ annotations, but an annotation is usually just a shortcut for a more basic, manual operation.

I'm not familiar with the specific library/annotations you are using, but from the looks of it, @SessionScoped will just inject the bean into the user's session attributes 'automagically' for you.

A user's session is just a map that is active while a particular user is logged in. You could put any kind of java object in the session attributes map, it doesnt need to be a special kind of object or anything. A 'session scoped bean' is basically fancy words for 'a java object which has been added to the user's session attributes map.'

When the user's session ends, the request.getSession() object is destroyed, along with the objects in the attributes map (so long as they are not referenced anywhere else) thats why they are 'session scoped'.

Also, you might want to try and put @SessionScoped @Named on seperate lines, I'm not sure it will parse them on one line like that.