Session 在JSF中设置/获取会话属性

Session 在JSF中设置/获取会话属性,session,jsf,authentication,servlet-filters,Session,Jsf,Authentication,Servlet Filters,我试图在JSF应用程序中实现简单的登录功能。接下来,我实现了一个AuthenticationFilter。我正在尝试将托管bean中的对象设置为: FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); session.setAttribute("use

我试图在JSF应用程序中实现简单的登录功能。接下来,我实现了一个AuthenticationFilter。我正在尝试将托管bean中的对象设置为:

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
session.setAttribute("user", user);
AuthenticationFilter的doFilter方法如下所示:

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

       if (((HttpServletRequest) req).getSession().getAttribute("user") == null){   
            ((HttpServletResponse) resp).sendRedirect("../login.jsf");

       } else {
         chain.doFilter(req, resp);
       }
   }
我总是得到
((HttpServletRequest)req).getSession().getAttribute(“用户”)==null
(true)。我已经搜索并应用了许多备选方案,比如(在我的bean中):


我不知道怎么处理这件事。似乎也帮不上忙。我做错了什么?我怎样才能让它工作?使用JSF功能有没有一种好的、干净的方法

我建议您像前面的答案一样使用安全库。有太多的方法不正确地做到这一点


但是如果你下定决心要自己做,不要在课程中设置这个。声明ManagedBean并将其作为会话级别进行作用域。将bean的属性设置为用户名。

浏览器是否在请求之间正确维护
JSESSIONID
cookie?跟踪HTTP流量。
facesContext.getExternalContext().getSessionMap().put("user", user);
request.getSession().setAttribute("user", user);
session.getServletContext().setAttribute("user", user); // DISASTER