如何从 Symfony 5 中的请求获取防火墙名称?
How to get firewall name from Request in Symfony 5?
问题很简单。我正在实施 AccessDeniedListener
并得到一个 ExceptionEvent
对象。从这里我可以得到请求。仅当我位于 security.yaml.
中定义的防火墙之一时,我才想应用某些逻辑
如何从 ExceptionEvent
或 Request
个实例中获取防火墙名称?
编辑:
我发现此代码“有效”
$firewall_context_name = $event->getRequest()->attributes->get('_firewall_context');
不过我不是很高兴。 There should be 一个 FirewallContext
或 FirewallConfig
对象可以以某种方式检索,不是吗?
谢谢
class AccessDeniedListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
// the priority must be greater than the Security HTTP
// ExceptionListener, to make sure it's called before
// the default exception listener
KernelEvents::EXCEPTION => ['onKernelException', 2],
];
}
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!$exception instanceof AccessDeniedException) {
return;
}
$request = $event->getRequest();
// HOW TO GET FIREWALL NAME HERE???
security.yaml
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api/
security: false
main:
custom_authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: app_logout
lazy: true
provider: app_user_provider
如文档中所述 you linked:
This object can be accessed through the getFirewallConfig(Request $request)
method of the
Symfony\Bundle\SecurityBundle\Security\FirewallMap
class
这个 class 不能直接注入,所以你必须使用服务别名 security.firewall.map
在 services.yaml
中配置你的依赖(或者如果你打算创建一个服务别名在其他地方使用它)。
services:
# ...
App\Listener\AccessDeniedListener:
arguments:
$firewallMap: '@security.firewall.map'
现在修改您的侦听器以接收此参数:
class AccessDeniedListener implements EventSubscriberInterface
{
private $firewallMap;
public function __construct(FirewallMapInterface $firewallMap)
{
$this->firewallMap = $firewallMap;
}
// Ommited getSubscribedEvents
public function onKernelException(ExceptionEvent $event): void
{
$request = $event->getRequest();
$firewallConfig = $this->firewallMap->getFirewallConfig($request);
if (null === $firewallConfig) {
return;
}
$firewallName = $firewallConfig->getName();
}
}
问题很简单。我正在实施 AccessDeniedListener
并得到一个 ExceptionEvent
对象。从这里我可以得到请求。仅当我位于 security.yaml.
如何从 ExceptionEvent
或 Request
个实例中获取防火墙名称?
编辑: 我发现此代码“有效”
$firewall_context_name = $event->getRequest()->attributes->get('_firewall_context');
不过我不是很高兴。 There should be 一个 FirewallContext
或 FirewallConfig
对象可以以某种方式检索,不是吗?
谢谢
class AccessDeniedListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
// the priority must be greater than the Security HTTP
// ExceptionListener, to make sure it's called before
// the default exception listener
KernelEvents::EXCEPTION => ['onKernelException', 2],
];
}
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!$exception instanceof AccessDeniedException) {
return;
}
$request = $event->getRequest();
// HOW TO GET FIREWALL NAME HERE???
security.yaml
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api/
security: false
main:
custom_authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: app_logout
lazy: true
provider: app_user_provider
如文档中所述 you linked:
This object can be accessed through the
getFirewallConfig(Request $request)
method of theSymfony\Bundle\SecurityBundle\Security\FirewallMap
class
这个 class 不能直接注入,所以你必须使用服务别名 security.firewall.map
在 services.yaml
中配置你的依赖(或者如果你打算创建一个服务别名在其他地方使用它)。
services:
# ...
App\Listener\AccessDeniedListener:
arguments:
$firewallMap: '@security.firewall.map'
现在修改您的侦听器以接收此参数:
class AccessDeniedListener implements EventSubscriberInterface
{
private $firewallMap;
public function __construct(FirewallMapInterface $firewallMap)
{
$this->firewallMap = $firewallMap;
}
// Ommited getSubscribedEvents
public function onKernelException(ExceptionEvent $event): void
{
$request = $event->getRequest();
$firewallConfig = $this->firewallMap->getFirewallConfig($request);
if (null === $firewallConfig) {
return;
}
$firewallName = $firewallConfig->getName();
}
}