2012-12-27 39 views
0

我为我想保护的任何页面配置了认证过滤器。 然而,当它试图重定向到登录页面,我遇到下面的错误JSF重定向导致过滤器中的异常

com.sun.faces.context.FacesFileNotFoundException 

..here是我的过滤器

@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 

任何人都知道的原因是什么?

回答

0

该异常表示JSF无法找到视图。你的项目是否有这个目录结构:contextRoot/faces/pages/public/login.xhtml?

0

/faces路径前缀通常默认添加到某些IDE(即NetBeans)的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; 
    }