Failed to get session from FacesContext inside Servlet filter

After authorizing my user, I want to put the link in the session to the current registered user.

This is how I do it in the setCurrentUser method:

 FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); session.setAttribute("CURRENT_USER", currentUser); 

Unfortunately, the session link is always null!

Alternatively I tried using sessionMap

 FacesContext facesContext = FacesContext.getCurrentInstance(); Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap(); sessionMap.put("CURRENT_USER", currentUser); 

This is a terrible failure with this exception:

 java.lang.UnsupportedOperationException at java.util.AbstractMap.put(AbstractMap.java:186) (...) 

What am I doing wrong?

The full code of my controller

UserController.java

 public class UserController implements Filter { private FilterConfig fc; private static final String CURRENT_USER = "CURRENT_USER"; public void init(FilterConfig filterConfig) throws ServletException { fc = filterConfig; log(">> Filter initialized"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Authenticate user // ... // Save refernce in Session setCurrentUser(currentUser); //(...) } public static void setCurrentUser(User u) { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); session.setAttribute(CURRENT_USER, u);// session is always NULL } public static User getCurrentUser() { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); return (User)session.getAttribute(CURRENT_USER); } //... } 

Jsf 2.0
JBoss 5.1.0.GA

+4
source share
2 answers

FacesContext not available in Filter because Filter is called before the FacesServlet .

Instead, you should get the session from the request argument.

 HttpSession session = ((HttpServletRequest) request).getSession(); session.setAttribute("user", currentUser); // ... 

Once you are in the JSF context (for example, inside a JSF managed bean or JSF view), then getSessionMap() will be available by the attribute name itself

 User user = (User) externalContext.getSessionMap().get("user"); 

Or just #{user} in EL:

 @ManagedProperty("#{user}") private User user; 
+11
source

Here is what I did:

 public class SesionUtil implements Serializable { private static final long serialVersionUID = 1L; public void setSesionUsuario(String key, Object value) { FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(key, value); } public String getSesionUsuario(String key) { return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(key).toString(); } } 

And then, to initialize the session, I simply did:

 @Inject // Inject dependency private SesionUtil sessionUtil; ... sessionUtil.setSesionUsuario("nombreUsuario",nombre); 
-1
source

All Articles