Symfony 2中的kernel.request事件中的身份验证令牌始终为空?

时间:2012-06-23 08:20:44

标签: events authentication symfony

我想在Symfony 2中为kernel.request事件编写基本监听器。服务定义非常简单,注释来自JMSDiExtraBundle。

问题是 $context->getToken()始终为空,即使用户已完全通过身份验证:

/**
 * @Service("request.set_messages_count_listener")
 *
 */
class RequestListener
{

    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    /**
     * @InjectParams({"container" = @Inject("service_container")})
     *
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * @Observe("kernel.request", priority = 255)
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $context = $this->container->get('security.context');
        var_dump($context->getToken()); die();
    }

}

我认为我的安全设置工作正常。那可能是什么问题呢?

secured_area:
    pattern:    ^/app/
    switch_user: true
    form_login:
        check_path: /app/login_check
        login_path: /app/login
        default_target_path: /app/dashboard
        always_use_default_target_path: true
    logout:
        path:   /demo/secured/logout # TODO
        target: /demo/               # TODO

access_control:
    - { path: ^/app/login,    roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/app/users,    roles: ROLE_MNG_USERS }
    - { path: ^/app/messages, roles: ROLE_MNG_USERS }
    - { path: ^/app/roles,    roles: ROLE_MNG_PACKAGES_FEATURES }
    - { path: ^/app/packages, roles: ROLE_MNG_PACKAGES_FEATURES }
    - { path: ^/app/,         roles: ROLE_USER }

1 个答案:

答案 0 :(得分:21)

使用priority = 255,在安全防火墙(priority = 8look here)之前调用您的侦听器。

尝试更改优先级。