【问题标题】:Is it possible to catch ViewExpriedException in PhaseListener?是否可以在 PhaseListener 中捕获 ViewExpriedException?
【发布时间】:2012-07-26 09:23:06
【问题描述】:

出于日志记录的目的,我有兴趣检测我的 JSF 应用程序中何时发生会话超时。

我已经实现了一个 PhaseListener 来检查用户是否已登录并且他们的会话是否已经处于活动状态。我对 afterPhase 方法的实现是:

  • var url_accepted(在代码中使用)包含用户应该有权访问以提供登录表单的公共页面列表。

    public void afterPhase(PhaseEvent event) {
        FacesContext context = event.getFacesContext();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
    
        AuthenticationBean sessionBean = (AuthenticationBean) session.getAttribute("sessionBean");
    
        String url_req = context.getViewRoot().getViewId();
    
        //1. Check if user has a session and is logged in:
        if(((sessionBean == null) || (sessionBean != null && !sessionBean.isLoggedIn())) && !url_accepted.contains(url_req)){
            context.getApplication().getNavigationHandler().handleNavigation(context,null,"auth_error");
            return;
        }
    
    //2. Code continues in order to check if a logged user has permissions to access the requested page(not relevant):        
    }
    

当用户由于会话超时而断开连接时,PhaseListener 无法从 ExternalContext 中检索我的 sessionBean 并将 null 分配给 sessionBean 属性。此时我无法区分用户是之前没有登录过还是超时断开连接。

我了解到可以使用 errorPages 来检测 ViewExpiredException 异常并将视图重定向到特定页面。但我不知道是否可以在我的源代码中管理这个异常。

我的问题是:我可以在 PhaseListener 实现中捕获这个 ViewExpiredException 以处理会话超时吗?

提前致谢。

【问题讨论】:

    标签: java jsf richfaces session-timeout jsf-1.2


    【解决方案1】:

    我在 JSF 项目中遇到过同样的情况。解决方案是使用过滤器来捕获会话过期。 BalusC(JSF 专家)解释了这个问题并举了一个很好的例子:

    另外,不要忘记在您的注销方法和会话超时处理程序中添加session.invalidate()

    【讨论】:

    • 感谢您的参考和建议。我会看看我是否可以在我的项目中调整这个 BalusC 解决方案。
    • 如果您有任何问题,请更新您的帖子并在此处发表评论,以便我们为您提供帮助。
    • 我一直在分析 BalusC 提出的示例,但我不明白他在哪里管理超时注销。我认为他没有采取任何行动。我在我的应用程序中遇到的问题已经适用于会话超时,但我需要在它发生时捕捉它,以便我可以记录哪个用户因此而断开连接。如果它已经在 BalusC 代码中实现,请告诉我,我将更详细地研究代码。
    猜你喜欢
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 2018-09-17
    • 2014-12-20
    • 2011-06-18
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多