3

1 週間前、私は ViewExpiredException について調べ、それに関するいくつかの記事を読みました。

私の問題は、場合によっては無視したいと思いViewExpiredExceptionます。これらは「セッション」を必要としない状況であり、私の Beans は@RequestScoped. 例として、ページlogin.xhtmlregister.xhtmlおよびpasswordRecovery.xhtml.

このような場合、セッションの有効期限が切れたというエラーがユーザーに表示されるのは非常に奇妙です。そのため、ログインページを開いてしばらく立ち止まっていると、彼があなたのデータを通知してログインをクリックすると、エラーページに転送されます. 私はそれを無視して、ユーザーに透過的にします。

したがって、これまでの私の解決策は、ExceptionHandlerこれらの例外を無視するために a を作成することです。

@Override
public void handle() throws FacesException {
    for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        // just remove the exception from queue
        if (t instanceof ViewExpiredException) {
            i.remove();
        }
    }
    getWrapped().handle();
}

次に、ユーザーがログインしているかどうかを確認するフィルターを作成し、ログインしていない場合はログイン ページにリダイレクトします (このフィルターは、認証が必要なページのみに適用されます)。

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    if (!loginManagedBean.isLogged()) {
        String pathLogin = request.getContextPath() + "/" + LOGIN_VIEW;
        if (isAJAXRequest(request)) {
            response.setContentType("text/xml");
            response.getWriter()
                    .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                    .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", pathLogin);
            return;
        }

        pathLogin += "?source=" + request.getServletPath();

        response.sendRedirect(pathLogin);
        return;
    }

    chain.doFilter(request, response);
}

そのため、セッションの有効期限が切れても、ログイン ページと登録ページでのユーザー エクスペリエンスには影響しません。そして、セッションを希望するページでは、フィルターによって処理されます。

それは良い解決策でしょうか?ViewExpiredExceptionで無視するセキュリティ リスクはありExceptionHandlerますか?

4

1 に答える 1