无法从Servlet过滤器内的FacesContext检索会话 [英] Failed to retrieve Session from FacesContext inside a Servlet Filter

查看:98
本文介绍了无法从Servlet过滤器内的FacesContext检索会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对用户进行身份验证后,我想在会话中添加对当前登录用户的引用.

这是我在setCurrentUser方法中执行的操作:

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

不幸的是,session引用始终为null!

或者,我尝试使用sessionMap

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

由于这个异常而失败了:

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

我在做什么错了?

我的控制器的完整代码

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

解决方案

由于FilterFacesServlet之前被调用,因此FacesContextFilter中不可用. /p>

您应该改为从request参数获取会话.

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

一旦您处于JSF上下文中(例如,在JSF托管Bean或JSF视图中),那么getSessionMap()将使用完全相同的属性名称提供此功能

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

或者只是通过EL中的#{user}:

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

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

解决方案

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;

这篇关于无法从Servlet过滤器内的FacesContext检索会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆