加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Symfony 2.8 – > 3.4升级IsGranted(‘IS_AUTHENTI

发布时间:2021-07-06 05:21:17 所属栏目:大数据 来源: https://www.jb51.cc
导读:我正在将Symfony从2.8升级到3.4并且我有一个Authentication Listener. 监听器的构造函数 public function __construct(EntityManager $entityManager, SessionInterface $session, Security $security, LoggerInterface $logger, Redis $redis, $secret) { $

我正在将Symfony从2.8升级到3.4并且我有一个Authentication Listener.

监听器的构造函数

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

        }
    }

Service.yml

app.token_listener:
    class: InseadMIMBundleListenerAuthTokenListener
    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'),
    )

即时尝试使用用户名和密码访问登录路由,但我得到以下错误

GENERAL EXCEPTION: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. in

/var/www/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authorization/AuthorizationChecker.PHP line 55 
Exception caught by Listener::  
[
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Security.PHP",
    "line": 65,
    "function": "isGranted",
    "class": "SymfonyComponentSecurityCoreAuthorizationAuthorizationChecker",
    "type": "->",
    "args": [
      "IS_AUTHENTICATED_ANONYMOUSLY",
      null
    ]
  },
  {
    "file": "/var/www/src/Insead/MIMBundle/Listener/AuthTokenListener.PHP",
    "line": 135,
    "function": "isGranted",
    "class": "SymfonyComponentSecurityCoreSecurity",
    "type": "->",
    "args": [
      "IS_AUTHENTICATED_ANONYMOUSLY"
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.PHP",
    "line": 212,
    "function": "onRequest",
    "class": "InseadMIMBundleListenerAuthTokenListener",
    "type": "->",
    "args": [
      null,
      "kernel.request",
      null
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.PHP",
    "line": 44,
    "function": "doDispatch",
    "class": "SymfonyComponentEventDispatcherEventDispatcher",
    "type": "->",
    "args": [
      [
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onRequest"
        ],
        [
          null,
          "onController"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "configure"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onKernelRequest"
        ],
        [
          null,
          "onRequest"
        ]
      ],
      "kernel.request",
      null
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.PHP",
    "line": 127,
    "function": "dispatch",
    "class": "SymfonyComponentEventDispatcherEventDispatcher",
    "type": "->",
    "args": [
      "kernel.request",
      null
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.PHP",
    "line": 68,
    "function": "handleRaw",
    "class": "SymfonyComponentHttpKernelHttpKernel",
    "type": "->",
    "args": [
      {
        "attributes": null,
        "request": null,
        "query": null,
        "server": null,
        "files": null,
        "cookies": null,
        "headers": null
      },
      1
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.PHP",
    "line": 200,
    "function": "handle",
    "class": "SymfonyComponentHttpKernelHttpKernel",
    "type": "->",
    "args": [
      {
        "attributes": null,
        "request": null,
        "query": null,
        "server": null,
        "files": null,
        "cookies": null,
        "headers": null
      },
      1,
      true
    ]
  },
  {
    "file": "/var/www/web/app.PHP",
    "line": 29,
    "function": "handle",
    "class": "SymfonyComponentHttpKernelKernel",
    "type": "->",
    "args": [
      {
        "attributes": null,
        "request": null,
        "query": null,
        "server": null,
        "files": null,
        "cookies": null,
        "headers": null
      }
    ]
  }
]

我已经花了好几天时间,我仍然无法解决这个问题.

我很抱歉,如果这已经回答我试图搜索的问题,我尝试了各种帖子中提到的事情,但它没有解决它.我也是symfony的新手.

完整的Security.PHP

https://www.codepile.net/pile/7O1LJkpv

AuthTokenListner.PHP

https://www.codepile.net/pile/Xv1ZMlAP

解决方法:

将AuthorizationChecker注入您的班级

protected $authChecker;

public function __construct(AuthorizationChecker $authChecker)
    {
        $this->authChecker = $authChecker;
    }

通过在service.yml中注入它

XXXXXXXXX:
    class: AppXXXXXXXXXXXX
    arguments: [ "@security.authorization_checker" ]

然后使用它来检查使用isGranted的角色

if ($this->authChecker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {

}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读