Failed to retrieve Session from FacesContext inside a Servlet Filter

↘锁芯ラ 提交于 2019-12-21 05:30:48

问题


After autheticating my user, I want to put a reference in the session to current logged in user.

Here 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 reference is always null !

Alternatively, I tried with the sessionMap

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

It miserably failed 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


回答1:


The FacesContext is not available in a Filter as the Filter is invoked before the FacesServlet.

You should be getting the session from the request argument instead.

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

Once you're in JSF context (e.g. inside a JSF managed bean or a JSF view), then this will be available by getSessionMap() on the very same attribute name

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

Or just by #{user} in EL:

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



回答2:


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 executed:

@Inject // Inject dependency
private SesionUtil sessionUtil;

...

sessionUtil.setSesionUsuario("nombreUsuario",nombre);


来源:https://stackoverflow.com/questions/11014105/failed-to-retrieve-session-from-facescontext-inside-a-servlet-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!