通过idlemonitor超时(primefaces) [英] Timeout via idlemonitor (primefaces)

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

问题描述

我正在尝试通过 idlemonitor 处理会话超时,一个主要组成部分。

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 放在我的 loggedntmplate中。 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");

}

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

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