2012-12-27 44 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; 
    }