为了记录目的,我有兴趣检测何时在我的JSF应用程序中发生会话超时。是否有可能在PhaseListener中捕获ViewExpriedException?
我已经实现了一个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和空分配给一个sessionBean属性。在这一点上,我无法分辨用户之前是否已经登录过,或者由于超时而断开连接。
我读过,人们可以为了检测ViewExpiredException异常并重定向视图到指定页面使用errorPages。但我不知道是否有可能在我的源代码中管理这个异常。
我的问题是:我可以在我的PhaseListener实现中捕获这个ViewExpiredException以处理会话超时?
在此先感谢。
感谢您的参考和建议。我会看看我是否可以在我的项目中调整这个BalusC解决方案。 – jmrodrigg 2012-07-27 11:14:11
如果您有任何问题,只需更新您的文章并在此留言,以便我可以帮助您。 – 2012-07-27 15:05:31
我一直在分析BalusC提出的例子,但是我没有得到他管理超时注销的地方。我认为他没有采取任何行动。 我在应用程序中遇到的问题已经适用于会话超时,但我需要赶上它的发生时间,以便我可以记录哪个用户因此而断开连接。 让我知道它是否已经在BalusC代码中实现,我将更详细地介绍代码。 – jmrodrigg 2012-07-31 12:43:55