从事件订阅者重定向
Redirect from event subscriber
我想在 kernel.controller 事件上从我的事件订阅者重定向用户条件,并在用户无法访问请求时更改它。
<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class WebPageAccessSuscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
private $session;
/**
* Contient l'ensemble des parametres de config
*
* @var ParameterBagInterface
*/
private $params;
private $router;
public function __construct(SessionInterface $session, ParameterBagInterface $params, RouterInterface $router)
{
// Retrieve the client session
$this->session = $session;
// Retrieve configuration variable
$this->params = $params;
$this->router = $router;
}
public function onKernelController(ControllerEvent $event)
{
error_log(__METHOD__);
$controller = $event->getController();
if(!is_array($controller)) return;
if ($this->params->get('visitor_access') === false && $controller[0] instanceof \App\Controller\restictedController) {
$event->setController(function() use ($event) {
return new RedirectResponse($this->router->generate('login_' . $event->getRequest()->getLocale()));
});
}
}
public static function getSubscribedEvents()
{
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::CONTROLLER => [['onKernelController']]
];
}
}
它可以工作,但 symfony 网络分析器不再显示。
我是说确实如此,但他的内容来自我的登录模板
为什么 symfony 将 webprofiler 重定向到登录页面?如何才能防止这种行为?我应该使用其他方法来实现吗?
如果您想保护应用程序的整个部分,请使用 symfony 安全组件创建一个具有自己身份验证的防火墙。
如果您想保护特定页面,您可以使用安全投票器https://symfony.com/doc/current/security/voters.html
编辑:通过投票者拒绝访问后,您可以使用 https://symfony.com/doc/current/security/access_denied_handler.html
重定向用户
我想在 kernel.controller 事件上从我的事件订阅者重定向用户条件,并在用户无法访问请求时更改它。
<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class WebPageAccessSuscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
private $session;
/**
* Contient l'ensemble des parametres de config
*
* @var ParameterBagInterface
*/
private $params;
private $router;
public function __construct(SessionInterface $session, ParameterBagInterface $params, RouterInterface $router)
{
// Retrieve the client session
$this->session = $session;
// Retrieve configuration variable
$this->params = $params;
$this->router = $router;
}
public function onKernelController(ControllerEvent $event)
{
error_log(__METHOD__);
$controller = $event->getController();
if(!is_array($controller)) return;
if ($this->params->get('visitor_access') === false && $controller[0] instanceof \App\Controller\restictedController) {
$event->setController(function() use ($event) {
return new RedirectResponse($this->router->generate('login_' . $event->getRequest()->getLocale()));
});
}
}
public static function getSubscribedEvents()
{
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::CONTROLLER => [['onKernelController']]
];
}
}
它可以工作,但 symfony 网络分析器不再显示。 我是说确实如此,但他的内容来自我的登录模板
为什么 symfony 将 webprofiler 重定向到登录页面?如何才能防止这种行为?我应该使用其他方法来实现吗?
如果您想保护应用程序的整个部分,请使用 symfony 安全组件创建一个具有自己身份验证的防火墙。
如果您想保护特定页面,您可以使用安全投票器https://symfony.com/doc/current/security/voters.html
编辑:通过投票者拒绝访问后,您可以使用 https://symfony.com/doc/current/security/access_denied_handler.html
重定向用户