[英]Tell whether route is behind firewall in Symfony2
I'm currently writing an event listener in Symfony2, which listens for the kernel.response
event, and adds a cookie to it if: a) a user is logged in, and b) no such cookie currently exists.我目前正在 Symfony2 中编写一个事件侦听器,它侦听
kernel.response
事件,并在以下情况下kernel.response
添加 cookie:a) 用户已登录,并且 b) 当前不存在此类 cookie。 It takes the service container as an argument.它将服务容器作为参数。
However, I'm getting an error when the listener responds to events not behind a firewall (such as those in the dev toolbar) since the token is empty and an AuthenticationCredentialsNotFoundException
is thrown.但是,当侦听器响应不在防火墙后面的事件(例如开发工具栏中的事件)时,我收到错误消息,因为令牌为空并且抛出了
AuthenticationCredentialsNotFoundException
。 However, I can't for the life of me figure out how to tell whether the route is behind a firewall or not.但是,我终生无法弄清楚如何判断路由是否在防火墙后面。 Could anyone help?
有人可以帮忙吗?
Code代码
public function onKernelResponse(FilterResponseEvent $event) {
// does the request have a device cookie?
if ($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')
&& !$this->getRequest()->cookies->has(DeviceManager::COOKIE_PREFIX.'id')) {
// no. Create one.
$DeviceManager = $this->container->get('salus_user.device_manager');
$Cookie = $DeviceManager->createDeviceCookie();
$Response = $event->getResponse();
$Response->headers->setCookie($Cookie); // and save it
}
// else, yes, we don't need to do anything
}
Error错误
AuthenticationCredentialsNotFoundException in classes.php line 2888:
The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.
First check if token exist:首先检查令牌是否存在:
public function onKernelResponse(FilterResponseEvent $event) {
if (!$this->container->get('security.token_storage')->getToken()) {
return;
}
// Rest of code.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.