无法从Servlet过滤器内的FacesContext检索会话 [英] Failed to retrieve Session from FacesContext inside a Servlet Filter
问题描述
对用户进行身份验证后,我想在会话中添加对当前登录用户的引用.
这是我在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
由于 您应该改为从 一旦您处于JSF上下文中(例如,在JSF托管Bean或JSF视图中),那么 或者只是通过EL中的 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 Unfortunately, the Alternatively, I tried with the sessionMap It miserably failed with this exception : What am I doing wrong ? The full code of my controller UserController.java JSF 2.0 The You should be getting the session from the Once you're in JSF context (e.g. inside a JSF managed bean or a JSF view), then this will be available by Or just by
这篇关于无法从Servlet过滤器内的FacesContext检索会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Filter
在FacesServlet
之前被调用,因此FacesContext
在Filter
中不可用. /p>
request
参数获取会话.HttpSession session = ((HttpServletRequest) request).getSession();
session.setAttribute("user", currentUser);
// ...
getSessionMap()
将使用完全相同的属性名称提供此功能User user = (User) externalContext.getSessionMap().get("user");
#{user}
:@ManagedProperty("#{user}")
private User user;
setCurrentUser
method : FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
session.setAttribute("CURRENT_USER", currentUser);
session
reference is always null !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)
(...)
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);
}
//...
}
JBoss 5.1.0.GAFacesContext
is not available in a Filter
as the Filter
is invoked before the FacesServlet
.request
argument instead.HttpSession session = ((HttpServletRequest) request).getSession();
session.setAttribute("user", currentUser);
// ...
getSessionMap()
on the very same attribute nameUser user = (User) externalContext.getSessionMap().get("user");
#{user}
in EL:@ManagedProperty("#{user}")
private User user;