【发布时间】: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