2012-06-23 25 views
7

Tôi đang cố gắng viết một trình nghe cơ bản cho sự kiện kernel.request trong Symfony 2. Định nghĩa dịch vụ khá đơn giản và các chú thích đến từ JMSDiExtraBundle.Mã xác thực luôn rỗng trong sự kiện kernel.request trong Symfony 2?

Những vấn đề là $context->getToken() luôn là null ngay cả những người sử dụng được đầy đủ chứng thực:

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

} 

Tôi nghĩ rằng thiết lập an ninh của tôi là làm việc tốt. Điều gì có thể là vấn đề sau đó?

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 } 

Trả lời

19

Với priority = 255, người nghe của bạn được gọi TRƯỚC tường lửa bảo mật (priority = 8, look here).

Cố gắng thay đổi mức độ ưu tiên của bạn.

+0

Cảm ơn, hiện đang hoạt động. – gremo

+0

Hãy xem tại đây https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Http/Firewall.php#L93, cảm ơn. :) – umpirsky

Các vấn đề liên quan