Symfony 2.8 -> 3.4 升级 IsGranted

我正在将 Symfony 从 2.8 升级到 3.4,并且我有一个身份验证侦听器。


监听器的构造器


  public function __construct(EntityManager $entityManager, SessionInterface $session, Security $security, LoggerInterface $logger, Redis $redis, $secret)

    {

        $this->entityManager    = $entityManager;

        $this->session          = $session;

        $this->security         = $security;

        $this->logger           = $logger;

        $this->redis            = $redis;

        $this->secret           = $secret;

    }

在侦听器中调用的请求函数


       public function onRequest(GetResponseEvent $event)

        {


        //Validate token


        //Get Authorization Header

        $headers = $event->getRequest()->headers;

        $authHeader = $headers->get('Authorization');


        //Check if Header value starts with 'Bearer'

        if($this->startsWith($authHeader, self::$BEARER_HEADER)) {


                // Allow request to be processed by controllers

               //token handler



        } else {


            $securityContext = $this->security;

            if ($securityContext->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {

                return;

            } else {

                throw new SessionTimeoutException();

            }


        }

    }

服务.yml


app.token_listener:

    class: Insead\MIMBundle\Listener\AuthTokenListener

    arguments: ["@doctrine.orm.entity_manager", "@session", "@security.helper", "@logger", "@redis.authtoken", "%secret%"]

    tags:

        - { name: kernel.event_listener, event: kernel.request, method: onRequest, priority: 0 }

ACL 列表条目 - security.php


  'access_control' => array(

       array('path' => '^/api/(.*?)/login', 'role'=>'IS_AUTHENTICATED_ANONYMOUSLY'),

    )


ibeautiful
浏览 188回答 3
3回答

宝慕林4294392

bin/console debug:event-dispatcher kernel.request帮助解决了这个问题。我正在使用tags:        - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse, priority: 10 }在 Services.yml 中,它与getSubscribedEvents()因此我删除了标签,只保留了public static function getSubscribedEvents()    {        return array(            KernelEvents::REQUEST  => array('onKernelRequest', 10),        );    }然后我通过给予其他两个监听器高优先级来将身份验证监听器移到下方,就像在 symfony 2.8 中一样

慕尼黑8549860

将AuthorizationChecker注入您的班级protected $authChecker;public function __construct(AuthorizationChecker $authChecker)    {        $this->authChecker = $authChecker;    }通过将其注入您的service.ymlXXXXXXXXX:    class: App\XXX\XXXX\XXXXX    arguments: [ "@security.authorization_checker" ]然后使用它使用isGranted检查角色if ($this->authChecker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {}
打开App,查看更多内容
随时随地看视频慕课网APP