Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication token always null in kernel.request event in Symfony 2?

I'm tring to write a basic listener for kernel.request event in Symfony 2. Service definition is pretty simple and annotations come from JMSDiExtraBundle.

The problems is that $context->getToken() is always null even the user is fully authenticated:

/**
 * @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();
    }

}

I think my security setup is working fine. What could be the problem then?

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 }
like image 555
gremo Avatar asked Jun 23 '12 08:06

gremo


1 Answers

With priority = 255, your listener is called BEFORE the security firewall (priority = 8, look here).

Try to change your priority.

like image 114
Olivier Dolbeau Avatar answered Nov 14 '22 04:11

Olivier Dolbeau