[英]Symfony: A Token was not found in the SecurityContext for route behind firewall
[英]Tell whether route is behind firewall in Symfony2
我目前正在 Symfony2 中编写一个事件侦听器,它侦听kernel.response
事件,并在以下情况下kernel.response
添加 cookie:a) 用户已登录,并且 b) 当前不存在此类 cookie。 它将服务容器作为参数。
但是,当侦听器响应不在防火墙后面的事件(例如开发工具栏中的事件)时,我收到错误消息,因为令牌为空并且抛出了AuthenticationCredentialsNotFoundException
。 但是,我终生无法弄清楚如何判断路由是否在防火墙后面。 有人可以帮忙吗?
代码
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
}
错误
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.
首先检查令牌是否存在:
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.