Silex:设置防火墙会话生存期

时间:2013-01-30 09:52:32

标签: symfony silex

我正在尝试在用户登录后延长默认生命周期。对于登录,我使用安全服务提供商,如下所示:

    $app = $this->_app;             
    $this->_app->register(new Silex\Provider\SecurityServiceProvider(), array(
        'security.firewalls' => array(
            'default' => array(
                'pattern' => '^.*$',
                'anonymous' => true, // Needed as the login path is under the secured area
                'form' => array('login_path' => '/signup/', 'check_path' => 'login_check', 'failure_path' => 'login_failure'),
                'logout' => array('logout_path' => '/logout/'), // url to call for logging out
                'users' => $this->_app->share(function() use ($app)
                    {
                        // Specific class App\User\UserProvider is described below
                        return new UserProvider($app['db']);
                    }),
            ),
        ),
        'security.access_rules' => array(
            array('^/restricted/$', 'ROLE_USER'),
        )
    ));

我已尝试设置会话生命周期(cookie),如下所示:

    $this->_app->register(new Silex\Provider\SessionServiceProvider(), array(
        'session.storage.options' => array('cookie_lifetime' => (60 * 60 * 12)), // 12 hours
    ));

但仍然没有。会话在15分钟左右后自动删除。

如何将登录安全防火墙的生命周期延长至12小时?

2 个答案:

答案 0 :(得分:1)

我想我终于明白了:

在数据库中保存会话似乎解决了这个问题。

SQL:

  CREATE TABLE `session` (
      `session_id` varchar(255) NOT NULL,
      `session_value` text NOT NULL,
      `session_time` int(11) NOT NULL,
      PRIMARY KEY (`session_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

PHP:

    /* SESSION IN DB */
    $this->_app->register(new Silex\Provider\SessionServiceProvider());
    $this->_app['session.db_options'] = array(
        'db_table' => 'session',
        'db_id_col' => 'session_id',
        'db_data_col' => 'session_value',
        'db_time_col' => 'session_time',
    );
    $this->_app['session.storage.handler'] = $this->_app->share(function ()
        {
            return new PdoSessionHandler(
                $this->_app['db']->getWrappedConnection(), $this->_app['session.db_options'], $this->_app['session.storage.options']
            );
        });

答案 1 :(得分:1)

如果您不想在DB中存储会话,这是一个解决方案:只需在php.ini中增加session.gc_maxlifetime

当会话存储在文件中时,它们(默认情况下)会被放到/var/lib/php/sessions/目录中。很明显,该目录必须不时被清除。为了实现这一点,在/etc/cron.d/php5配置了一个cron作业,每30分钟触发一次脚本/usr/lib/php5/sessionclean。此脚本接受php配置并从那里获取session.gc_maxlifetime,然后删除早于此变量中指定的文件。

问题是:默认情况下session.gc_maxlifetime等于1440秒或24分钟。您可以将其增加到适合您的任何值,例如24小时(并按会话cookie生命周期限制您的会话)。