通过PrimeFaces p:idleMonitor超时 [英] Timeout via PrimeFaces p:idleMonitor

查看:101
本文介绍了通过PrimeFaces p:idleMonitor超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 idlemonitor 处理会话超时, Primefaces组件.

I am trying to handle the session timeout via idlemonitor, a primefaces component.

我这样做是因为我需要让用户知道由于不活动,会话已过期.我需要通过对话框显示此消息,他关闭对话框后,应将其重定向到登录页面.他应该不能单击后退"并在应用程序上浏览,就像什么也没有发生一样.如果他单击后退",则应将其重定向到sessionexpired.xhtml页面.

I am doing this because i need to let the user know that due to inactivity the session has expired. I need to display this message through a dialog, after he closes the dialog he should be redirected to the loginpage. He should not be able to click "back" and browse on the application just as nothing happend; if he clicks "back" he should be redirected to a sessionexpired.xhtml page.

我将 idleMonitor 放入了 loggedintemplate.xhtml 中,因此,仅当您登录后,无论您在哪个页面上,因为我所有的页面,它都可以正常工作登录后,则来自 loggedintemplate.xhtml .

I put the idleMonitor in my loggedintemplate.xhtml, so it works only if you are logged in, no matter on what page, beacause all my pages, after you logged in, derives from the loggedintemplate.xhtml.

这是我的 loggedintemplate.xhtml 中的代码的样子:

This is how the code in my loggedintemplate.xhtml looks like:

<p:idleMonitor timeout="6000" onidle="idleDialog.show()" />

<p:dialog header="Timeout" resizable="false" closable="false" 
          widgetVar="idleDialog" modal="true">
    <p:panel styleClass="noborderpanel">
        <p:panelGrid columns="1" styleClass="adressegrid">
            <p:outputLabel value="Session has expired due to inactivity" />
                    <p:commandButton action="#{loginController.timeout()}"
                        value="Ok" />
        </p:panelGrid>
    </p:panel>
</p:dialog>

因此,此代码的功能基本上是检查用户是否在6秒钟内处于非活动状态,如果他处于非活动状态,则会弹出一个无法关闭的对话框,并告诉他会话已过期.

So the functionality of this code basically checks if the user was inactive for 6 seconds, if he was inactive, a un-closeable dialog pops up and tells him that the session has expired.

方法 loginController.timeout()应该注销用户,使会话无效等.

The method loginController.timeout() should log the user out, invalidate the session etc.

我的问题是我不知道如何使会话无效,如何注销用户等.如果我使用FacesContext.getCurrentInstance().getExternalContext() .invalidateSession();,则确实会使会话无效,但是我还需要更多.例如,如果用户超过30分钟(默认的JavaEE超时时间)处于非活动状态,则会得到nullPointerException.

My problem is that I don't know how to invalidate the session, how to log out the user etc. If I use FacesContext.getCurrentInstance().getExternalContext() .invalidateSession(); it does invalidate the session, but I need more. For example, if the user is inactive for more than 30 min, the default JavaEE timeout time, i get a nullPointerException.

我想手动"处理超时,有没有办法禁用默认JavaEE超时?

I want to handle the timeout "manually", is there a way to disable the default JavaEE timeout?

手动处理超时的最佳方法是什么,而不是像这样:

What is the best way to handle the timeout manually, not like this:

<session-config>
    <session-timeout>20</session-timeout>
</session-config>   
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/expired.xhtml</location>
</error-page>

推荐答案

我已经找到了一种非常不错的解决方案,而且我也不必处理NPE.

I've found a solution that works pretty nice, and i don't have to deal with the NPE.

以下情况:

如果用户闲置时间超过6秒钟,则通过ajax使会话无效,不需要用户交互.这意味着,即使用户闲置了20分钟以上,该会话也已经无效,并且我不必处理NPE.

If the user is idle for more than 6 sec, the session is invalidated through ajax, no user interaction required. That means, even if the user is idle for more than 20 minutes, the session is already invalid and I don't have to deal with the NPE.

6秒后,将生成一个对话框(通过javascript警报),并让用户知道他在一段时间内处于非活动状态,并且会话已过期.关闭对话框后,用户将被重定向到登录页面.

After 6 seconds a dialog(through javascript alert) is generated and lets the user know that he was inactive for a certain amount of time and the session has expired. After the dialog is closed, the user is redirected to the login page.

顺便说一句,我只花了6秒钟进行测试.我的默认设置是:

By the way, i used 6 seconds just for testing purposes. My default settings are:

  • idlemonitor: 30分钟
  • web.xml: 40分钟(只是为了确保会话始终手动无效,并且用户可以看到包含会话过期信息的对话框)
  • idlemonitor: 30 min
  • web.xml: 40 min (just to ensure that the session is always invalidated manually and the users gets to see the dialog with the session expired information)

此解决方案完全符合我的要求.

This solution matches exactly to my requirements.

idlemonitor的代码:

<!-- language: lang-xml -->

<p:idleMonitor timeout="1800000" >
    <p:ajax event="idle" listener="#{loginController.timeout()}" oncomplete="alert('Session expired etc.')"/>
</p:idleMonitor>

loginController.timeout()的代码:

public void timeout() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext()
            .invalidateSession();
    FacesContext.getCurrentInstance().getExternalContext()
            .redirect("...loginpage.xhtml");

}

这篇关于通过PrimeFaces p:idleMonitor超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆