Timeout via PrimeFaces p:idleMonitor

前端 未结 1 1410
旧时难觅i
旧时难觅i 2021-01-16 05:19

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

I am doing this because i need to let the user know that due to inactivity the se

相关标签:
1条回答
  • 2021-01-16 06:02

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

    Following scenario:

    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.

    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.

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

    • 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.

    Code for idlemonitor:

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

    Code for loginController.timeout():

    public void timeout() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext()
                .invalidateSession();
        FacesContext.getCurrentInstance().getExternalContext()
                .redirect("...loginpage.xhtml");
    
    }
    
    0 讨论(0)
提交回复
热议问题