在我的 JSF 应用程序中,有一个Filter
用于检查身份验证过程。当认证失败时,过滤器重定向到login.xhtml
.
如何将 传递FacesMessage
给我的登录页面Filter
?
虽然,我在下面使用过,但不行。
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
AuthenticationFilter.java
public class AuthenticationFilter implements Filter {
private FilterConfig config;
private ServletContext servletContext;
public void init(FilterConfig filterConfig) {
config = filterConfig;
servletContext = config.getServletContext();
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
HttpSession session = httpRequest.getSession();
User user = (User) session.getAttribute(Constants.LOGIN_USER);
if (user == null) {
...
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
String loginView = httpRequest.getContextPath() + Constants.LOGIN_PAGE;
httpResponse.sendRedirect(loginView);
} else if (!user.getRole().equals(Role.SYSTEM_ADMINISTRATOR)) {
....
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
String loginView = httpRequest.getContextPath() + Constants.LOGIN_PAGE;
httpResponse.sendRedirect(loginView);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
servletContext.log("Exiting the filter");
}
public void destroy() {
//Nothing to do.
}
}