Symfony 2.8 -> 3.4 升级 IsGranted('IS_AUTHENTICATED_ANONYMOUSLY') 抛出错误

2023-11-26

我正在将 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'),
    )

我尝试使用用户名和密码访问登录路由,但出现以下错误

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": "Symfony\\Component\\Security\\Core\\Authorization\\AuthorizationChecker",
    "type": "->",
    "args": [
      "IS_AUTHENTICATED_ANONYMOUSLY",
      null
    ]
  },
  {
    "file": "/var/www/src/Insead/MIMBundle/Listener/AuthTokenListener.php",
    "line": 135,
    "function": "isGranted",
    "class": "Symfony\\Component\\Security\\Core\\Security",
    "type": "->",
    "args": [
      "IS_AUTHENTICATED_ANONYMOUSLY"
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php",
    "line": 212,
    "function": "onRequest",
    "class": "Insead\\MIMBundle\\Listener\\AuthTokenListener",
    "type": "->",
    "args": [
      null,
      "kernel.request",
      null
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php",
    "line": 44,
    "function": "doDispatch",
    "class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
    "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": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
    "type": "->",
    "args": [
      "kernel.request",
      null
    ]
  },
  {
    "file": "/var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php",
    "line": 68,
    "function": "handleRaw",
    "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
    "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": "Symfony\\Component\\HttpKernel\\HttpKernel",
    "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": "Symfony\\Component\\HttpKernel\\Kernel",
    "type": "->",
    "args": [
      {
        "attributes": null,
        "request": null,
        "query": null,
        "server": null,
        "files": null,
        "cookies": null,
        "headers": null
      }
    ]
  }
]

我已经花了好几天的时间来解决这个问题,但我仍然无法弄清楚如何解决它。

很抱歉,如果这个问题已经得到解答,我尝试搜索并尝试了各种帖子中提到的内容,但没有解决它。我也是 symfony 的新手。

完全安全.php

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

AuthTokenListner.php

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


我相信这是已被弃用/删除的 securitycontext。 isGranted 需要在授权检查器上调用

return $this->get('security.authorization_checker');

您需要“security.authorization_checker”服务。

然后,您可以在authorization_checker 服务上调用isGranted。

// get the service from the container or pass it in via injection
$authChecker = $this->get('security.authorization_checker');
if ($authChecker->isGranted('IS...')) { ... }

我使用了ector以更容易迁移。我强烈推荐https://github.com/rectorphp/rector以便顺利迁移。我可以保证使用这个工具您会节省大量时间。

https://www.tomasvotruba.cz/blog/2019/02/28/how-to-upgrade-symfony-2-8-to-3-4/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Symfony 2.8 -> 3.4 升级 IsGranted('IS_AUTHENTICATED_ANONYMOUSLY') 抛出错误 的相关文章

  • 垃圾邮件打败了我所有的验证码

    我有一个 WordPress 博客 我在上面使用验证码插件的时间最长 因为它有效 最近我收到了大量的垃圾邮件 然后我尝试执行通过 PHP 生成的 将这些随机数添加在一起 但仍然没有成功 我不太确定需要做什么来阻止这些垃圾邮件 但这很烦人 我
  • 将 OAuth WRAP 访问令牌直接保存在客户端计算机上的 cookie 中吗?

    我计划建立一个可以访问 oauth 包装框架的网站 我正在考虑将访问令牌按原样存储在客户端计算机上 我不想在服务器上维护临时令牌等数据库 我应该做吗 或者我应该加密它 首先 为什么他们不使用 OAuth 2 0 您可以将 OAuth 凭据存
  • 无法在jspdf中加载多个图像

    我正在尝试加载动态生成的多个图像 我想将这些图像转换为 PDF 格式 HTML 代码如下
  • Zend Framework 中的动态默认模块

    有谁知道在 Zend Framework 中动态设置默认模块并且不会遇到命名空间问题的方法 例如 我想要做的是有一个允许加载的模块表 其中一个设置为默认模块 例如 我可能有 admin blog calendar 作为可以加载的模块 如果我
  • 如何在spring-security的SecurityContext中存储自定义信息?

    在我的应用程序中 我使用 LDAP 身份验证 但我还有 2 个远程服务 需要通过登录方法 用户名 密码 进行身份验证 该方法返回安全令牌 这使我能够调用其他方法 即我应该将安全令牌作为第一个参数传递给服务方法 所以我想在使用 LDAP 成功
  • PHP 的脚手架 [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 PHP 中有什么东西可以像 Rails 一样创建基本的脚手架吗 编辑 我需要一些东西来快速原型化 一些框架比如Symfony http www sym
  • 为 NFL api 生成访问令牌

    NFL 有一个 API 服务 link https api nfl com docs getting started index html https api nfl com docs getting started index html
  • PHP DOM - 剥离 span 标签,保留其内容

    我希望采用如下标记 span class test Some text that is strong bolded strong and contains a a href link a span 并在 PHP 中找到剥离跨度的最佳方法 剩
  • 切换到 mysqli 是个好主意吗?

    我正在考虑为我的所有 php 项目切换到 mysqli 我的代码编写方式 我运行非常简单的网站并构建了自己的基本框架 我在所有网站上使用该框架 我在修改函数和类时不应该遇到太多问题 然而 我只听说过关于准备好的语句的积极的事情 除了一些关于
  • json_encode 返回 NULL?

    由于某种原因 项目 描述 返回NULL使用以下代码 这是我的数据库的架构 CREATE TABLE staff id int 11 NOT NULL AUTO INCREMENT name longtext COLL
  • Laravel 登录后重定向回来

    登录后如何重定向返回页面 在 Laravel 5 2 中 认证控制器 protected redirectTo 重定向用户
  • 使用 C# 登录《我的世界》

    我正在尝试为自己和一些朋友创建一个简单的自定义 Minecraft 启动器 我不需要启动 Minecraft 的代码 只需要登录的实际代码行 例如 据我所知 您过去可以使用 string netResponse httpGET https
  • Paypal 将钱从一个帐户转移到另一个帐户

    我知道这个建议如何汇款至任何 PayPal 账户 https stackoverflow com questions 1559808 paypal api send money to any paypal account但到目前为止我所尝试
  • PHP Intl 扩展线程安全吗?

    我一直在阅读有关 PHP 中的语言环境的内容 看起来setlocale 线程有问题 我对线程不太熟悉 文档提到它不是线程安全的 我想让我的项目能够处理某些数字格式 并且 Intl 扩展似乎很有趣 http php net manual en
  • symfony easyadmin 自定义表单生成器

    我使用 symfony 3 4 和 easycorp easyadmin bundle 1 17 配置表单 easyadmin form fields type group label Basic Information icon enve
  • ACL授权失败后ZF3重定向

    我有一个带有 ACL 的新 ZF3 应用程序 现在 我需要在未经授权的访问的情况下重定向到错误页面 例如 403 我认为最好的方法是触发一个事件 然后捕获它 但我失败了 全部都在我的用户模块中Module php 摘录 namespace
  • 如何将图像从 Android 应用程序上传到网络服务器的特定文件夹中

    如何将图像从 android 移动到 Web 服务器上的指定文件夹 这是我的安卓代码 package com example bitmaptest import java io ByteArrayOutputStream import ja
  • 通过移动应用程序使用 Moodle 进行身份验证

    我的移动应用程序需要登录 Moodle 以从 Web 服务获取 Json 数据并使用 Angular 显示它 为此 我需要传入用户名和密码并取回 Moodle Web 服务令牌 因此我的应用程序不需要再次登录 至少在令牌过期之前 这是 提出
  • 通过 Sparkpost 发送 iCal 邀请

    我正在尝试使用 SparkPost 通过电子邮件以附件形式发送日历邀请 但收到电子邮件后邀请不会打开 我使用两个文件 calendarinvite php 来创建邀请 使用 Sendemail php 来发送电子邮件 calendarinv
  • 我可以让 swagger-php 在查询字符串上使用数组吗?

    我使用 Swagger php 当我定义查询字符串上的参数时 它可以是一个数组 但据我所知 它不支持这种查询字符串 https api domain tld v1 objects q 1 q 5 q 12 我相信这会被设定in the co

随机推荐