2011-02-05 40 views
1

我已經在web.xml中定義像以下的過濾器: -Servlet過濾器將在無限循環時在映射FORWARD用於JSF

<filter-mapping> 
    <filter-name>AuthenticationFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>ERROR</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
</filter-mapping> 
<filter> 
    <display-name>AuthenticationFilter</display-name> 
    <filter-name>AuthenticationFilter</filter-name> 
    <filter-class>com.filters.AuthenticationFilter</filter-class> 
</filter> 

和在濾波器我已經以下代碼: -

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
    // TODO Auto-generated method stub 
    // place your code here 

    HttpServletRequest httpRequest = (HttpServletRequest) request; 
    HttpServletResponse hres = (HttpServletResponse) response; 


    String pathInfo = httpRequest.getRequestURI().trim(); 

    System.out.println(pathInfo); 

    // Do not process any non-jsp files or LogIn.jsp ! pathInfo.endsWith("jsf") || 
    if (pathInfo.endsWith("RegistrationForm.jsf") || pathInfo.endsWith("Login.jsf")) { 
    chain.doFilter(request, response); 
    return; 
    } 
    // pass the request along the filter chain 
    User user = (User) httpRequest.getSession().getAttribute("USER_IN_SESSION"); 
    if(user==null) 
    hres.sendRedirect("Login.jsf"); 
    else { 
    chain.doFilter(request, response); 

    } 

} 

的問題是,如果我調用應用程序與Topic.jsp它遍歷這樣的: -

Topic.jsp 
LogIn.jsf 
Login.jsp 
Login.jsf 
Login.jsp 
... 

我發現這個問題爲t他在映射中前進。如果刪除此條目,它的工作原理

<dispatcher>FORWARD</dispatcher> 

請幫我解決無限循環交替.JSP & .jsf :)

回答

1

理論這個puzzzle:

  1. 主題.jsp需要驗證,所以 重定向到LogIn.jsf。

  2. LogIn.jsf由 FacesServlet提供服務。但是包含臉部的 的頁面實際上是jsp 頁面。所以servlet向LogIn.jsp(構建 組件樹的頁面)發送了一個 。

  3. 在你的過濾器,路徑是login.jsp的,所以你問 認證和重定向到 LogIn.jsf又使你根本無法驗證的login.jsp一個 請求。轉至步驟2

因此,如果您刪除,FacesServlet的從LogIn.jsf到loginForm.jsp文件轉發並不能在循環進入。

快速解決方案:將LogIn.jsp添加到if語句中的路徑信息列表中。

+0

感謝勝利者的洞察力:) – anand 2011-02-12 05:11:34

1

首先,爲什麼你想在forward/include/error分派掛鉤以及旁邊(默認)全球和全覆蓋request

擺脫過濾器映射中所有那些<dispatcher>行。你不需要任何一個認證過濾器。 HTTP請求是唯一重要的。內部正向/包含/錯誤分派只能在HTTP請求已經到達(並被過濾)時發生。

此外,您也可以將篩選器映射到更具體的<url-pattern>,如/secured/*左右,並將所有需要登錄的頁面放在那裏,然後將註冊和登錄頁面放在外面。

+0

感謝Balus的解釋和提示:) – anand 2011-02-12 05:09:51