[英]JSF Redirect causes exception in a filter
我已经为要保护的所有页面配置了身份验证筛选器。 但是,当它尝试重定向到登录页面时,遇到以下错误
com.sun.faces.context.FacesFileNotFoundException
..这是我的过滤器
@WebFilter(filterName = "Authentication Filter", urlPatterns = { "/pages/*" }, dispatcherTypes = {
DispatcherType.REQUEST, DispatcherType.FORWARD })
public class AuthenticationFilter implements Filter {
static final Logger logger = Logger.getLogger(AuthenticationFilter.class);
private String contextPath;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if (httpRequest.getUserPrincipal() == null) {
httpResponse.sendRedirect(contextPath
+ "/faces/pages/public/login.xhtml");
return;
}
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
contextPath = fConfig.getServletContext().getContextPath();
}
}
..,并将我的web.xml与此人servlet的代码映射
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
不确定,但我已验证该路径在我的项目文件夹中存在
+pages
+public
-login.xhtml
生成的路径是
http://localhost:8080/MyApp/faces/pages/public/login.xhtml
有人知道原因吗?
异常表明JSF无法找到视图。 您的项目是否具有以下目录结构:contextRoot / faces / pages / public / login.xhtml?
通常,某些IDE(即NetBeans)默认将/faces
路径前缀添加到faces url-pattern中。 您可能已从web.xml对其进行了更改,但尚未从过滤器sendRedirect参数中删除它。
为了使您的过滤器正常工作,请从过滤器中的sendRedirect()
方法中删除/faces
前缀:
httpResponse.sendRedirect(contextPath + "/pages/public/login.xhtml");
或像这样将其添加到web.xml中:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
最后,请注意您的过滤器不会引起无限循环。 在重定向之前添加此检查可能会很有用:
HttpServletRequest req = (HttpServletRequest) request;
if (!req.getRequestURI().contains("/pages/public/login.xhtml") && httpRequest.getUserPrincipal() == null) {
// redirect
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.