Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSF重定向导致筛选器中出现异常_Java_Jsf 2_Servlet Filters - Fatal编程技术网

Java JSF重定向导致筛选器中出现异常

Java JSF重定向导致筛选器中出现异常,java,jsf-2,servlet-filters,Java,Jsf 2,Servlet Filters,我已经为我想要保护的任何页面配置了身份验证过滤器。 然而,当它试图重定向到登录页面时,我遇到了下面的错误 com.sun.faces.context.FacesFileNotFoundException 这是我的过滤器 @WebFilter(filterName = "Authentication Filter", urlPatterns = { "/pages/*" }, dispatcherTypes = { DispatcherType.REQUEST, Dispatch

我已经为我想要保护的任何页面配置了身份验证过滤器。 然而,当它试图重定向到登录页面时,我遇到了下面的错误

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被映射为Facesservlet的以下代码

<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>
生成的路径是

http://localhost:8080/MyApp/faces/pages/public/login.xhtml

有人知道原因吗?

异常表明JSF无法定位视图。您的项目是否具有以下目录结构:contextRoot/faces/pages/public/login.xhtml?

/faces
路径前缀通常由某些IDE(即NetBeans)默认添加到faces url模式中。您可能已从web.xml更改了它,但尚未从filter sendRedirect参数中删除if

要使筛选器正常工作,请从筛选器中的
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>
<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;
    }