会话到期后自动注销 [英] Auto Log Off once the session expires

查看:176
本文介绍了会话到期后自动注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序在30分钟后注销并被重定向到登录页面,我在web.xml中指定会话超时并使用requestProcessor进行重定向。我想向用户显示一条消息,说明会话在会话后过期到期,我怎么能这样做。自动注销?
我想提示页面上的错误消息会话超时,请再次登录。然后,我怎么能检测到会话超时?任何方法会自动触发吗?

Our application logs off after 30 min and gets redirected to login page,i am specifying session timeout in web.xml and using a requestProcessor for redirecting.I want to show to the user a message saying your session got expired once the session expires,how can i do that.Auto log off ? I would like to prompt the error message on the page"The session is timeout, please login again" . Then how could I detect the session is timeout? will any methods trigger automatically?

推荐答案

创建一个活动检查器,检查每一分钟是否发生任何用户活动(mouseclick,keypress )并在服务器端执行心跳以在用户处于活动状态时使会话保持活动状态,并在用户未处于活动状态时不执行任何操作。如果30分钟内没有活动(或者在服务器端设置了默认会话超时),则执行重定向。

Create an activity checker which checks every minute if any user activity has taken place (mouseclick, keypress) and performs a heartbeat to the server side to keep the session alive when the user is active and does nothing when the user is not active. When there is no activity for 30 minutes (or whatever default session timeout is been set on server side), then perform a redirect.

这是一个启动示例,几乎没有任何帮助 jQuery 绑定点击和按键事件并触发ajax请求。

Here's a kickoff example with little help of jQuery to bind click and keypress events and fire ajax request.

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function() {
        $.active = false;
        $('body').bind('click keypress', function() { $.active = true; });
        checkActivity(1800000, 60000, 0); // timeout = 30 minutes, interval = 1 minute.
    });

    function checkActivity(timeout, interval, elapsed) {
        if ($.active) {
            elapsed = 0;
            $.active = false;
            $.get('heartbeat');
        }
        if (elapsed < timeout) {
            elapsed += interval;
            setTimeout(function() {
                checkActivity(timeout, interval, elapsed);
            }, interval);
        } else {
            window.location = 'http://example.com/expired'; // Redirect to "session expired" page.
        }
    }
</script>

创建一个 Servlet ,它监听 / heartbeat 基本上只做以下几点:

Create a Servlet which listens on /heartbeat and does basically just the following:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    request.getSession();
}

保持会话活着。

当您将登录用户存储在会话中时,只要会话过期,它就会自动注销。因此,您无需手动注销用户。

When you store the logged-in user in the session, it will be "automagically" logged out whenever the session expires. So you don't need to manually logout the user.

这篇关于会话到期后自动注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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