1

I'm using Mojarra 2.1.29 and have a session scoped JSF managed bean

@ManagedBean
@SessionScoped
public class UserContext{

   //staff

}

and a spring bean:

public class UserReproducer{
    private User user;

    public void reporoduce(){
        if(user == null){
            //Here I need to recreate the userContext managed bean
            //do some other staff
        }
    }
}

In fact, I need some kind of custom scope in JSF, i.e. to re-create the userContext bean from scratch when the condition is satisfied. Is it possible to do in some way? By recreating, I mean cleaning all its properties down as it was created for the first time.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3663882
  • 6,957
  • 10
  • 51
  • 92

1 Answers1

-1

Invalidate the session, to destroy a session scoped bean:

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

Another option to log out from Spring Security is to clear the context:

SecurityContextHolder.clearContext();
Stefan
  • 12,108
  • 5
  • 47
  • 66
  • Is that session tied with spring security's session? I mean is that the same thing? – user3663882 Jun 08 '15 at 10:30
  • The SecurityContext object is saved within the http session yes. – Stefan Jun 08 '15 at 10:36
  • Is it possible to invalidate the JSF-session somehow when the user's trying to log in? – user3663882 Jun 08 '15 at 10:42
  • Why not give the class a `reset()` method? – Kukeltje Jun 08 '15 at 15:00
  • This solution invalidates the whole HTTP session. So it doesn't just clear the bean, but it invalidates the data from all the session and view managed beans linked to that HTTP session. It's not the most proper solution for the concrete question, what the OP asks can be done with @Kukeltje's workaround. – Aritz Jun 08 '15 at 16:08
  • The accepted answer is like shooting a mosquito with an elefant – Kukeltje Jun 08 '15 at 16:53
  • The lifetime of a sessionscoped bean ends, if the session ends. So you think its better to break the JSF scoping? – Stefan Jun 09 '15 at 06:43