Symfony2 getUser() 来自安全上下文的侦听器类失败,尽管令牌存在 [英] Symfony2 getUser() in Listener Class from Security Context failing although Token exists

查看:35
本文介绍了Symfony2 getUser() 来自安全上下文的侦听器类失败,尽管令牌存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是将当前登录的用户转储到 Web Profiler.

Objective is to get the currently logged on user dumped to web profiler.

我有一个注入了容器的监听器类:

I have a listener class with the Container injected:

namespace Bookboon\Premium\AppBundle\EventListener;

use Classes\geoPlugin;
use Bookboon\Premium\AppBundle\Controller;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\DependencyInjection\Container;

class CountryListener //extends FilterControllerEvent
{
    protected $_container;

    public function __construct(Container $container)
    {
        $this->_container = $container;
    }

    public function onKernelController(FilterControllerEvent $event)
    {
        $sess = $this->_container->get("session"); // Get the current session

        $user = $this->_container->get('security.context')->getToken()->getUser();

        dump($user); 
    }
}

就这样,当我访问它加载的网页时没有任何格式,我收到错误加载 Web 调试工具栏时发生错误(内部服务器错误:500)

With this as it stands when I visit the webpage it loads without any formatting and I receive the error 'An Error occurred while loading the web debug toolbar (Internal Server Error: 500)

是否要打开分析器?'

我单击确定并收到此错误:错误:调用非对象上的成员函数 getUser()"

I click ok and receive this error: 'Error: Call to a member function getUser() on a non-object'

但是,如果我有这样的代码:

However if I have the code like so:

namespace Bookboon\Premium\AppBundle\EventListener;

use Classes\geoPlugin;
use Bookboon\Premium\AppBundle\Controller;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\DependencyInjection\Container;

class CountryListener //extends FilterControllerEvent
{
    protected $_container;

    public function __construct(Container $container)
    {
        $this->_container = $container;
    }

    public function onKernelController(FilterControllerEvent $event)
    {
        $sess = $this->_container->get("session"); // Get the current session

        $user = $this->_container->get('security.context')->getToken(); // CHANGE IS HERE

        dump($user); 
    }
}

那么就没有问题了,token dump出来输出正常,肯定存在,页面格式正确.有什么想法吗?

Then there is no problem, tokens dumped to output fine and definitely exists and the page formatting is correct. Any ideas?

推荐答案

如果你看一下函数的文档 getToken(),你会看到当安全上下文有没有身份验证令牌.

If you have a look at the documentation of the function getToken(), you'll see that it returns NULL when the security context has no authentication token.

如果是这种情况,并且您尝试访问 NULL 的方法 getUser(),则会引发异常.这将解释为什么你的第一段代码失败而第二段没有.

If that is the case and you try to access the method getUser() of NULL, the exception is thrown. That would explain why your first piece of code is failing and the second is not.

这篇关于Symfony2 getUser() 来自安全上下文的侦听器类失败,尽管令牌存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆