繁体   English   中英

是否可以在PhaseListener中捕获ViewExpriedException?

[英]Is it possible to catch ViewExpriedException in PhaseListener?

出于日志记录的目的,我有兴趣检测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以便处理会话超时吗?

提前致谢。

我在JSF项目中也遇到过同样的情况。 解决方案是使用筛选器来捕获会话已过期。 BalusC(JSF专家)对此事进行了解释,并展示了一个很好的例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM