简体   繁体   English

Symfony2中的Token_storage没有令牌

[英]Token_storage in Symfony2 has no token

In Symfony2, I pass @security.token_storage to a listener that is registered as a service. 在Symfony2中,我将@ security.token_storage传递给注册为服务的侦听器。 I do this for multiple services. 我这样做是为了多种服务。

In one case, the token_storage does not have a method getToken(). 在一种情况下,token_storage没有方法getToken()。 I don't understand why, as a user is logged in and other services can access it. 我不明白为什么,因为用户已登录并且其他服务可以访问它。

I registered my service like this: 我这样注册了我的服务:

gedmo.listener.loggable:
    class: CmdbBundle\Library\Loggable\LoggableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]
        - [ setUserValue, [ @security.token_storage ] ]

I don't know why there is no token accessible. 我不知道为什么没有令牌可访问。 My dev bar shows that there is a token of the kind: UsernamePasswordToken. 我的开发栏显示有一种令牌:UsernamePasswordToken。

My intention is to obtain the username of the user that is currently logged in. I need to make the service aware of that. 我的意图是获取当前登录用户的用户名。我需要使服务意识到这一点。

The service does not appear to be initialized, the attribute in your listener is probably null at the moment you try to to get the token. 该服务似乎未初始化,在您尝试获取令牌时,侦听器中的属性可能为null。 I personally use constructor to construct services, in your case you must too try like this. 我个人使用构造函数来构造服务,在您的情况下,您也必须这样尝试。 http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services

Because setters are made for optional services, and in your case the service seems mandatory. 因为二传手是为可选服务创建的,所以在您看来,该服务是强制性的。

gedmo.listener.loggable:
    class: CmdbBundle\Library\Loggable\LoggableListener
    arguments:
        - '@annotation_reader'
        - '@security.token_storage'
    tags:
        - { name: doctrine.event_subscriber, connection: default }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM