【问题标题】:Symfony 2 SecurityContext class deprecatedSymfony 2 SecurityContext 类已弃用
【发布时间】:2015-06-18 19:36:57
【问题描述】:

当我尝试在 symfony 演示中访问应用程序/示例时出现以下错误

错误:Symfony\Component\Security\Core\SecurityContext 类是 自 2.6 版起已弃用,并将在 3.0 版中删除。采用 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage 或 Symfony\Component\Security\Core\Authorization\AuthorizationChecker 而是。

服务器返回正确答案,但状态码为 200。

我在 Google 上没有找到任何相关信息。有没有人遇到过这个错误和/或知道如何解决它?

【问题讨论】:

标签: security symfony


【解决方案1】:

说明

从 Symfony 2.6 开始,SecurityContext 被拆分为 TokenStorageAuthorizationChecker(参见:Symfony Blog - "New in Symfony 2.6: Security component improvements")。

这样做的主要原因是为了防止在将SecurityContext 注入您自己的服务时经常发生的循环引用。

解决方案

更改本身是 100% 向后兼容的(如链接的博客文章中所述),您只需重写您访问 SecurityContext 的方式。

// Symfony 2.5
$user = $this->get('security.context')->getToken()->getUser();
// Symfony 2.6
$user = $this->get('security.token_storage')->getToken()->getUser();

// Symfony 2.5
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { ... }
// Symfony 2.6
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { ... }

您可以简单地尝试通过在源代码(包括供应商目录)中对 security.contextSecurityContext 进行文本搜索来找到罪魁祸首。

但是正如您所说,您使用的是 vanilla Symfony 2.6,它似乎只是使用了一些即将被弃用的方法。所以你可以简单地使用这个......

解决方法

正如 Symfony 所做的那样,它通过触发 E_USER_DEPRECATED 错误而被弃用,您可以在启动 Symfony AppKernel 时简单地禁用它们:

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function __construct($environment, $debug) {
        // Keep error reporting like it was and disable only deprecation warnings.
        error_reporting(error_reporting() & (-1 ^ E_DEPRECATED));
        // ...
    }
}

我个人喜欢弃用警告,因为 Symfony 的变更日志往往会提供非常详细的信息,说明您需要如何更改代码以支持 Symfony 的未来版本,并且弃用警告通常在方法实际弃用前几个月触发。

【讨论】:

  • 感谢您非常清晰的回答。我也喜欢弃用的警告,问题是它们破坏了我的 newRelic 监控。
  • Flu 能否请您告知如何将注释安全系统与新课程一起使用?
  • @Abdel5 我认为最好自己提出这个问题,因为这里的问题完全是为了摆脱 OP 遇到的错误。
  • 谢谢@flu。你救了我几个小时!
【解决方案2】:

这不是一个正确的错误,只是一个警告。

不推荐使用的类是计划在未来版本(在本例中为 Symfony)中删除的类。

它建议您停止使用它,并为您指出新的(和替代的)类 TokenStorageAuthorizationChecker,它们将完全接管相同的任务。

【讨论】:

  • 问题是,我刚刚分叉并部署了symfony-standard,所以我不知道在哪里使用了已弃用的类。另外,虽然只是一个警告,但它完全破坏了我的新遗物监控:这就是我想摆脱它的原因。
  • 所以,这完全正常,Symfony 中有一些已弃用的代码。你可以试试 Symfony 2.7 或在 New Relic 下忽略。
  • “尝试 Symfony 2.7”是什么意思? symfony-standard repo 已经基于 Symfony 2.7,不是吗?
  • 但是,我仍然收到了这些已弃用的警告。是否可以配置 Symfony 来避免这些?
  • 你可以配置你的 php.ini 来忽略它们
【解决方案3】:

看到这个警告真烦人。同时,您不想关闭警告。所以我想举一个例子来改变你的代码来摆脱它可能是有用的。以下是我更改HWIOAuthBundleOAuthUtils 类的方法。 首先,我把/vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/config/oauth.html改成了这样:

<service id="hwi_oauth.security.oauth_utils" class="%hwi_oauth.security.oauth_utils.class%">
    <argument type="service" id="security.http_utils" />
    <argument type="service" id="security.context" />
    <argument>%hwi_oauth.connect%</argument>
</service>

到这里:

<service id="hwi_oauth.security.oauth_utils" class="%hwi_oauth.security.oauth_utils.class%">
    <argument type="service" id="security.http_utils" />
    <argument type="service" id="security.authorization_checker" />
    <argument>%hwi_oauth.connect%</argument>
</service>

现在我们必须在 /vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Security/OAuthUtils 类中更改它:

使用 Symfony\Component\Security\Core\SecurityContextInterface; ... /** * @var 安全上下文接口 */ 私人 $securityContext; /** * @param HttpUtils $httpUtils * @param SecurityContextInterface $securityContext * @param 布尔值 $connect */ 公共函数 __construct(HttpUtils $httpUtils, SecurityContextInterface $securityContext, $connect) { $this->httpUtils = $httpUtils; $this->securityContext = $securityContext; $this->connect = $connect; }

到这里:

使用 Symfony\Component\Security\Core\Authorization\AuthorizationChecker; ... /** * @var 授权检查器 */ 私人 $authorizationChecker; /** * @param HttpUtils $httpUtils * @param AuthorizationChecker $authorizationChecker * @param 布尔值 $connect */ 公共函数 __construct(HttpUtils $httpUtils, AuthorizationChecker $authorizationChecker, $connect) { $this->httpUtils = $httpUtils; $this->authorizationChecker = $authorizationChecker; $this->connect = $connect; }

然后我在使用securityContext 的地方进行了更改。将其替换为authorizationChecker

公共函数 getAuthorizationUrl(请求 $request,$name,$redirectUrl = null,数组 $extraParameters = 数组()) { $resourceOwner = $this->getResourceOwner($name); if (null === $redirectUrl) { if (!$this->connect || !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $redirectUrl = $this->httpUtils->generateUri($request, $this->ownerMap->getResourceOwnerCheckPath($name)); } 别的 { $redirectUrl = $this->getServiceAuthUrl($request, $resourceOwner); } } 返回 $resourceOwner->getAuthorizationUrl($redirectUrl, $extraParameters); }

之所以用 AuthorizationChecker 代替 SecurityContext 是因为这种情况下只使用了 isGranted 方法。如果您的情况需要,也许您可​​以将其替换为 TokenStorage 或同时使用 AuthorizationChecker 和 TokenStorage。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-13
    • 2023-03-28
    • 2015-05-05
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多