bvisonl/inactivity-bundle

Symfony2 Bundle 用于处理用户不活跃和会话空闲超时

v1.0.0 2017-08-13 22:51 UTC

This package is auto-updated.

Last update: 2024-09-11 07:36:39 UTC


README

简介

本组件的目的是为 Symfony2 中用户的登录会话提供一个“更好的”处理方式。同时处理多个打开的标签页和客户端空闲时间。

我要感谢 (https://github.com/JillElaine/jquery-idleTimeout) 提供了如何实现此功能的想法。遗憾的是,那个组件对我来说没有正常工作,而且它只是解决了问题的一部分,所以我决定创建一个同时包含客户端和服务器端的东西。

先决条件

此组件依赖于 store.js ==> https://github.com/marcuswestin/store.js/

安装

  1. 通过 composer 安装
composer require bvisonl/inactivitybundle 1.x
  1. 将组件添加到 AppKernel
 public function registerBundles()
    {
        $bundles = array(
        ...
        new Bvisonl\InactivityBundle\BvisonlInactivityBundle(),
        ...           
        );
    }
  1. 在您的 parameters.yml 中定义 bvisonl_session_timeout 参数
parameters:
    bvisonl_session_lifetime: 300 # 5 minutes

如果您正在使用 cookie_lifetime,请将其在 config.yml 中设置为一个非常高的值

session:
    ...        
    cookie_lifetime: 300000000000 # Set this value high enough and let the bundle handle the session inactivity

另外,如果您打算在 HTML 中使用此参数(如下所示),您必须在 config.yml 中的 twig 全局中定义它

# Twig Configuration
twig:
    ...
    globals:        
        bvisonl_session_lifetime: "%bvisonl_session_lifetime%"
  1. 将 JavaScript 添加到您的 HTML 中(例如,在 base.html.twig 中)
{% javascripts '@BvisonlInactivityBundle/Resources/public/js/inactivity.handler.js' %}
    <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
  1. 配置 JS 插件以检查用户活动
<!-- InactivityHandler Configuration -->
<script type="text/javascript">
    $(document).ready(function() {
        $.fn.bvisonlInactivityHandler({
            // This is the default configuration, in reality no parameter is required but check that the defaults are ok for you
            sessionTimeout: {{ bvisonl_session_lifetime }}, // Default is 300 (5 minutes)
            logoutUrl: "{{ path('fos_user_security_logout') }}", // Default is "/"
            pingServer: true, // Default is false
            pingUrl: '/bvisonl/inactivity/ping', // Url to ping to keep session alive in the server, MUST RETURN A 204 response
            pingInterval: {{ bvisonl_session_lifetime/2 }}, // Default is 150
            events: 'click keypress scroll wheel mousewheel mousemove keyup', // Default events to check on the browser
        });
    })
</script>
  1. 如果使用默认的 ping 函数,请将组件的路由添加到 routing.yml
# BvisonlInactivityBundle Routes
bvisonl_inactivity:
    resource: "@BvisonlInactivityBundle/Resources/config/routing.yml"
  1. 请记住清除您的缓存、安装资产,如果使用 assetic,请安装它们
php app/console assets:install --env=dev
php app/console assetic:dump --env=dev
php app/console cache:clear --env=dev

工作原理

基本来说,如果按照上述方式配置,过程分为两部分

~ 服务器

在服务器上,有一个监听器不断监听最后一次使用会话的时间,并将其与 session_lifetime 参数进行比较。如果应该销毁会话,则会销毁它。对于服务器来说很简单。

~ 客户端

在 Symfony2 中,如果您在可配置的时间内没有影响会话,则将销毁会话,并且您将无法对任何受保护的路由执行任何请求。但是,假设一个用户正在屏幕上分析一个非常复杂的报告,并且您的会话生命周期设置为 5 分钟,当用户完成报告的工作并尝试在应用程序中移动时,在下一个请求中,Symfony2 将将用户重定向到登录屏幕。

为了解决这个问题,插件负责跟踪配置的事件并维护计时器(使用 store.js)。此外,与 pingServer 功能结合使用,客户端将不断ping服务器(服务器应返回 204,200 不好,因为如果使用默认设置,Symfony2 可能会在重定向请求到登录屏幕时返回代码 200,您不需要担心这个问题)。每当客户端ping服务器时,它会告诉 Symfony2 的会话用户仍然活跃,因此不会将其注销。

待办事项

  • 可能:在注销前添加对话框窗口