2012-05-02 13 views
2

いくつかのファイルをスキップする(私はそれを変更することはできません):JSFフィルター、私はこのような構造でプロジェクトを持っている

Web Pages 
    META-INF 
    WEB-INF 
    assets (javascript, css and images) 
    includes (top.xhtml, bottom.xhtml, etc) 
    templates (master.xhtml) 
    views 
     fornecedor 
     -home.xhtml 
     -user.xhtml 
     -login.xhtml 
     franqueador 
     -home.xhtml 
     -user.xhtml 
     -login.xhtml 

Oが理由でフォルダごとにlogin.xhtmlが、私はそれを変更することはできません持って、それがプロジェクトマネージャーに渡されました。

これは私のSessionFilterです:

@WebFilter("/views/*") 
public class SessionFilter implements Filter { 

    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest request = (HttpServletRequest) req; 
     HttpServletResponse response = (HttpServletResponse) res; 
     HttpSession session = request.getSession(false); 
     LoginController loginController = (LoginController) ((boolean) (session != null) ? session.getAttribute("loginController") : null); 
     if (!request.getRequestURI().endsWith("/fornecedor/index.xhtml")) { 
      if (loginController == null || !loginController.getIsLoggedIn()) { 
       response.sendRedirect(request.getContextPath() 
         + "/views/index.xhtml"); 
      } else { 
       chain.doFilter(req, res); 
      } 
     } 
    } 


} 

そして、それは動作しませんが、ないhtmlコードと空白のページを返し、私は.html.xhtmlを変更すると、それは私にリダイレクトループを与えます。

login.xhtmlページとindex.xhtmlのすべてをviewsフォルダに保存する必要はありません。私はifのフィルタをデバッグしましたが、常にfalseを返します。 BalusC解答後

EDIT

私はこれに来た:

if (!request.getRequestURI().endsWith("/views/index.html") 
       && !request.getRequestURI().endsWith("/views/fornecedor/login.html") 
       && !request.getRequestURI().endsWith("/views/franqueado/login.html") 
       && (loginController == null || !loginController.getIsLoggedIn())) { 
      response.sendRedirect(request.getContextPath() + "/views/index.html"); 

     } else { 
      chain.doFilter(req, res); 
     } 

それが動作しているが、別の問題があり、私は10のフォルダを持っている場合、私はこの上でそれらを追加する必要がありますif声明...私はそれを自動化する必要があります。

答えて

2

最初のifの条件には、elseがありません。それは空白のページを説明します。

代わりにifにURLとログインした条件を評価する必要があります。リダイレクトループに関しては

if (!request.getRequestURI().endsWith("/fornecedor/index.xhtml") && (loginController == null || !loginController.getIsLoggedIn()) { 
    response.sendRedirect(request.getContextPath() + "/views/index.xhtml"); 
} else { 
    chain.doFilter(req, res); 
} 

あなたFacesServletが何らかの不明の理由で*.html代わりの*.xhtmlのURLパターンにマッピングされているので、それはです。したがって、要求URLは決して一致しません。それに応じてフィルタ内のURLを修正します。

if (!request.getRequestURI().endsWith("/index.html") 
    && !request.getRequestURI().endsWith("/login.html") 
    && (loginController == null || !loginController.getIsLoggedIn()) 
{ 
    response.sendRedirect(request.getContextPath() + "/views/index.html"); 
} else { 
    chain.doFilter(req, res); 
} 
+0

こんにちは、あなたの答えに感謝しかし、どのような '@WebFilter(「/ビュー/ *」)について':すべてのlogin.htmlindex.html要求のためにそれを一般化するには、単に次のようにありますか?このままにしておきますか? – Gerep

+0

このアノテーションは、フィルタをアプリケーションに登録し、 '/ views/*'と一致するURLパターンで実行できるようにします。これを削除すると、フィルタはまったく登録されないため、まったく実行されません。 – BalusC

+0

私の場合、私の 'views'フォルダ内にいくつかのフォルダがありますが、正しい選択はこのままにしておきます。 @WebFilter( "/ views/*") ' – Gerep

関連する問題