0

保護したいページに対して認証フィルターを構成しました。ただし、ログインページにリダイレクトしようとすると、以下のエラーが発生します

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 は、faces サーブレットのこのコードでマップされます

<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

誰も理由を知っていますか?

4

2 に答える 2

0

/facesパス接頭辞は通常、一部の IDE (つまり NetBeans) によってデフォルトで顔の url-pattern に追加されます。おそらく web.xml から変更しましたが、フィルタ sendRedirect 引数から if を削除していません。

フィルターを機能させるには、フィルターのメソッド/facesからプレフィックスを削除します。sendRedirect()

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;
    }
于 2012-12-27T09:42:30.290 に答える
0

この例外は、JSF がビューを見つけられないことを示しています。プロジェクトのディレクトリ構造は次のとおりです: contextRoot/faces/pages/public/login.xhtml?

于 2012-12-27T08:52:36.990 に答える