jsf - 忽略 ViewExpiredException 是错误的?

标签 jsf jakarta-ee viewexpiredexception

一周前,我研究了 ViewExpiredException,并阅读了几篇关于它的文章。

我的问题是,在某些情况下,我想忽略 ViewExpiredException。这些是不需要“ session ”的情况,我的 Beans 是 @RequestScoped。例如,页面 login.xhtmlregister.xhtmlpasswordRecovery.xhtml

在这些情况下,向用户显示您的 session 已过期的错误是非常奇怪的。所以如果你打开登录页面静止不动,当他通知你数据,点击登录时,就会跳转到一个错误页面。我会忽略它并让用户透明。

所以,到目前为止,我的解决方案是创建一个 ExceptionHandler 来忽略这些异常:

@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);
}

所以当session过期时,不会影响用户在登录和注册页面的体验。在我希望 session 的页面上,由过滤器处理。

那会是一个好的解决方案吗?忽略 ExceptionHandler 中的 ViewExpiredException 是否存在任何安全风险?

最佳答案

在这种特定情况下,忽略它们在技术上并不坏,但它表明设计不好。就好像您在工作中使用了错误的工具。 IE。这些 View 实际上应该永不过期。

只需专门使这些 View 无状态即可。

<f:view transient="true">
    ...
</f:view>

这可以放置在页面的任何位置,甚至可以重复,但大多数自文档都是将其作为页面、组合或定义的顶级标记。

另见:

关于jsf - 忽略 ViewExpiredException 是错误的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28675300/

相关文章:

session - ViewExpiredException,页面无法恢复

jsf - 将参数传递给 JSF 中的 View 作用域 bean

css - h :messages after migration from JSF 1. 1 到 JSF 1.2 的不同结果

java - 如何使用java检查Oracle过程是否正确创建?

java - 从 session 监听器访问和修改应用程序范围的托管 Bean 的属性

java - 使用自定义 ViewHandler 处理登录页面上的 ViewExpiredException

java - 如何同时显示模式和执行操作

java - com.sun.mail.smtp.SMTPAddressFailedException

java - 如何在 JPA/Hibernate 应用程序中配置连接池(没有 Spring)?