Symfony 2 SecurityContext 类已弃用 [英] Symfony 2 SecurityContext class deprecated

查看:54
本文介绍了Symfony 2 SecurityContext 类已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问 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 上没有找到任何相关信息.有没有人以前遇到过这个错误和/或知道如何修复它?

解决方案

说明

从 Symfony 2.6 开始,SecurityContext 被拆分为 TokenStorageAuthorizationChecker(参见:Symfony 博客 -Symfony 2.6 中的新功能:安全组件改进").

这样做的主要原因是为了防止在将 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.5if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { ... }//Symfony 2.6if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { ... }

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

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

解决方法

由于 Symfony 通过触发 E_USER_DEPRECATED 错误来弃用它,您可以在启动 Symfony AppKernel 时简单地禁用它们:

//app/AppKernel.php类 AppKernel 扩展内核{公共函数 __construct($environment, $debug) {//保持错误报告原样并仅禁用弃用警告.error_reporting(error_reporting() & (-1 ^ E_DEPRECATED));//...}}

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

I get the following error when I try to reach app/example on symfony demo

Error: The Symfony\Component\Security\Core\SecurityContext class is deprecated since version 2.6 and will be removed in 3.0. Use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage or Symfony\Component\Security\Core\Authorization\AuthorizationChecker instead.

The server is returning the right answer with a 200 status code though.

I've found nothing on Google about it. Has anybody encounter this error before and/or know how to fix it ?

解决方案

Explanation

Starting with Symfony 2.6 the SecurityContext got split into the TokenStorage and the AuthorizationChecker (see: Symfony Blog - "New in Symfony 2.6: Security component improvements").

The main reason for this was to prevent circular reference which occurred quite often when injecting the SecurityContext into your own services.

Solution

The change itself is 100% backwards compatible (as stated in the linked blog post), you just need to rewrite how you accessed the 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')) { ... }

You can simply try to find the culprit by doing a text-search for security.context or SecurityContext in your source code (including the vendor directory).

But as you stated that you're using vanilla Symfony 2.6 it seems that it simply uses some soon to be deprecated methods. So you might simply use this...

Workaround

As Symfony does it's deprecation by triggering E_USER_DEPRECATED errors, you can simply disable them when booting your 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));
        // ...
    }
}

I personally like the deprecation warnings, because Symfony's changelogs tend to give very detailed information on how you need to change your code to support future versions of Symfony and the deprecation warnings normally are triggered months before the methods are actually deprecated.

这篇关于Symfony 2 SecurityContext 类已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆