会话应该在 symfony2 中 10 分钟后注销
Session should logout after 10 min in symfony2
到目前为止我试过了,
路径 CoreBundle / DependencyInjection / configuration.php
namespace Funstaff\CoreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('funstaff_core');
$rootNode
->children()
->scalarNode('timeout')->defaultValue(900)
->isRequired()->end()
->end();
return $treeBuilder;
}
}
我们将在文件中添加这段代码FunstaffCoreExtension.php
namespace Funstaff\CoreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class FunstaffCoreExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container,
new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->setParameter('core.timeout', $config['timeout']);
}
}
在config.yml。
funstaff_core:
timeout: 600
我在以下路径中创建了 RequestListener.php 文件:FunstaffCoreBundle / Request / Listener。
namespace Funstaff\CoreBundle\Request\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class RequestListener implements EventSubscriberInterface
{
protected $session;
protected $securityContext;
protected $timeout;
/**
* Construct
*
* @param Session $session
*/
public function __construct(Session $session,
SecurityContext $securityContext,
$timeout)
{
$this->session = $session;
$this->securityContext = $securityContext;
$this->timeout = $timeout;
}
/**
* Get Subscribed Events
*
* @return array event list
*/
public static function getSubscribedEvents()
{
return array(
'kernel.request' => 'onKernelRequest',
);
}
/**
* On Kernel Request
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$meta = $this->session->getMetadataBag();
$lastused = $meta->getLastUsed();
if (null !== $lastused && (time() - $lastused) > $this->timeout) {
$this->securityContext->setToken(null);
$this->session->invalidate();
}
}
}
& 在资源/配置/services.yml
services:
timeout.request.listener:
class: Funstaff\CoreBundle\Request\Listener\RequestListener
arguments: [ @session, @security.context, %funstaff.timeout% ]
tags:
- { name: kernel.event_subscriber }
但它不是工作会话超时。
有什么东西留给我了吗?
请任何人有想法然后让我知道。
我找到了解决方案。
创建文件夹处理程序并在处理程序中创建文件。php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace my\DemoBundle\Handler;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\SecurityContextInterface;
/**
* Description of SessionIdleHandler
*
*/
class SessionIdleHandler {
protected $session;
protected $securityContext;
protected $router;
protected $maxIdleTime;
public function __construct(SessionInterface $session, SecurityContextInterface $securityContext, RouterInterface $router, $maxIdleTime = 0)
{
$this->session = $session;
$this->securityContext = $securityContext;
$this->router = $router;
$this->maxIdleTime = $maxIdleTime;
}
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
if ($this->maxIdleTime > 0) {
$this->session->start();
$lapse = time() - $this->session->getMetadataBag()->getLastUsed();
if ($lapse > $this->maxIdleTime) {
$this->securityContext->setToken(null);
$this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');
//Redirect URL to logout
$event->setResponse(new RedirectResponse($this->router->generate('logout')));
}
}
}
}
在service.yml文件中
services:
my.handler.session_idle:
class: my\DemoBundle\Handler\SessionIdleHandler
arguments: ["@session", "@security.context", "@router", %session_max_idle_time%]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
在parameter.yml
中设置参数(以秒为单位)
session_max_idle_time: 600
它完美地工作。
到目前为止我试过了,
路径 CoreBundle / DependencyInjection / configuration.php
namespace Funstaff\CoreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('funstaff_core');
$rootNode
->children()
->scalarNode('timeout')->defaultValue(900)
->isRequired()->end()
->end();
return $treeBuilder;
}
}
我们将在文件中添加这段代码FunstaffCoreExtension.php
namespace Funstaff\CoreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class FunstaffCoreExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container,
new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->setParameter('core.timeout', $config['timeout']);
}
}
在config.yml。
funstaff_core:
timeout: 600
我在以下路径中创建了 RequestListener.php 文件:FunstaffCoreBundle / Request / Listener。
namespace Funstaff\CoreBundle\Request\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class RequestListener implements EventSubscriberInterface
{
protected $session;
protected $securityContext;
protected $timeout;
/**
* Construct
*
* @param Session $session
*/
public function __construct(Session $session,
SecurityContext $securityContext,
$timeout)
{
$this->session = $session;
$this->securityContext = $securityContext;
$this->timeout = $timeout;
}
/**
* Get Subscribed Events
*
* @return array event list
*/
public static function getSubscribedEvents()
{
return array(
'kernel.request' => 'onKernelRequest',
);
}
/**
* On Kernel Request
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$meta = $this->session->getMetadataBag();
$lastused = $meta->getLastUsed();
if (null !== $lastused && (time() - $lastused) > $this->timeout) {
$this->securityContext->setToken(null);
$this->session->invalidate();
}
}
}
& 在资源/配置/services.yml
services:
timeout.request.listener:
class: Funstaff\CoreBundle\Request\Listener\RequestListener
arguments: [ @session, @security.context, %funstaff.timeout% ]
tags:
- { name: kernel.event_subscriber }
但它不是工作会话超时。 有什么东西留给我了吗? 请任何人有想法然后让我知道。
我找到了解决方案。
创建文件夹处理程序并在处理程序中创建文件。php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace my\DemoBundle\Handler;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\SecurityContextInterface;
/**
* Description of SessionIdleHandler
*
*/
class SessionIdleHandler {
protected $session;
protected $securityContext;
protected $router;
protected $maxIdleTime;
public function __construct(SessionInterface $session, SecurityContextInterface $securityContext, RouterInterface $router, $maxIdleTime = 0)
{
$this->session = $session;
$this->securityContext = $securityContext;
$this->router = $router;
$this->maxIdleTime = $maxIdleTime;
}
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
return;
}
if ($this->maxIdleTime > 0) {
$this->session->start();
$lapse = time() - $this->session->getMetadataBag()->getLastUsed();
if ($lapse > $this->maxIdleTime) {
$this->securityContext->setToken(null);
$this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');
//Redirect URL to logout
$event->setResponse(new RedirectResponse($this->router->generate('logout')));
}
}
}
}
在service.yml文件中
services:
my.handler.session_idle:
class: my\DemoBundle\Handler\SessionIdleHandler
arguments: ["@session", "@security.context", "@router", %session_max_idle_time%]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
在parameter.yml
中设置参数(以秒为单位)session_max_idle_time: 600
它完美地工作。