【发布时间】:2012-06-18 06:39:23
【问题描述】:
我正在尝试在 JSF 应用程序中实现简单的登录功能。在这个answer 之后,我实现了一个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("user") == null(真的)。我已经搜索并应用了许多 alternatives,例如(在我的 bean 中):
facesContext.getExternalContext().getSessionMap().put("user", user);
request.getSession().setAttribute("user", user);
session.getServletContext().setAttribute("user", user); // DISASTER
我不知道如何管理这件事。似乎duplicate question 也没有帮助。我究竟做错了什么?我怎样才能让它工作?有没有一种使用 JSF 功能的好方法?
【问题讨论】:
-
JSESSIONIDcookie 是否在浏览器的请求中正确维护?跟踪 HTTP 流量。
标签: session jsf authentication servlet-filters