Symfony 2 SecurityContext类已弃用
当我尝试访问symfony demo上的应用程序/示例时,出现以下错误 错误:Symfony\Component\Security\Core\SecurityContext类无效 自版本2.6起已弃用,将在3.0中删除。使用 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage 或Symfony\Component\Security\Core\Authorization\AuthorizationChecker 相反 服务器返回的是正确答案,状态代码为200Symfony 2 SecurityContext类已弃用,security,symfony,Security,Symfony,当我尝试访问symfony demo上的应用程序/示例时,出现以下错误 错误:Symfony\Component\Security\Core\SecurityContext类无效 自版本2.6起已弃用,将在3.0中删除。使用 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage 或Symfony\Component\Security\Core\Authorization\AuthorizationChe
我在谷歌上找不到这方面的信息。有没有人以前遇到过这个错误和/或知道如何修复它?这不是一个正确的错误,只是一个警告 不推荐使用的类是计划在将来的版本(在本例中是Symfony)中删除的类
它建议您停止使用它,并将您指向较新的(和替代品)类,
TokenStorage
和AuthorizationChecker
,它们将完全接管以完成相同的任务。解释
从Symfony 2.6开始,SecurityContext
被分为TokenStorage
和AuthorizationChecker
(请参阅:)
这样做的主要原因是防止在将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.context
或SecurityContext
进行文本搜索,即可找到罪魁祸首
但正如您所说的,您使用的是vanilla Symfony 2.6,它似乎只是使用了一些即将弃用的方法。所以你可以简单地使用这个
解决方法
正如Symfony通过触发E\u USER\u DEPRECATED
错误进行弃用一样,您可以在引导SymfonyAppKernel
时简单地禁用它们:
// 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,并且弃用警告通常在方法实际弃用数月之前触发。看到该警告会让人非常恼火。同时,您不想关闭警告。所以我想,举一个例子,改变你的代码来摆脱它也许是有用的。下面是我如何更改
HWIOAuthBundle
的OAuthUtils
类来实现这一点的。
首先,我将以下内容更改为/vendor/hwi/oauth bundle/hwi/bundle/oauth bundle/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>
use Symfony\Component\Security\Core\SecurityContextInterface;
...
/**
* @var SecurityContextInterface
*/
private $securityContext;
/**
* @param HttpUtils $httpUtils
* @param SecurityContextInterface $securityContext
* @param boolean $connect
*/
public function __construct(HttpUtils $httpUtils, SecurityContextInterface $securityContext, $connect)
{
$this->httpUtils = $httpUtils;
$this->securityContext = $securityContext;
$this->connect = $connect;
}
为此:
<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>
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
...
/**
* @var AuthorizationChecker
*/
private $authorizationChecker;
/**
* @param HttpUtils $httpUtils
* @param AuthorizationChecker $authorizationChecker
* @param boolean $connect
*/
public function __construct(HttpUtils $httpUtils, AuthorizationChecker $authorizationChecker, $connect)
{
$this->httpUtils = $httpUtils;
$this->authorizationChecker = $authorizationChecker;
$this->connect = $connect;
}
将SecurityContext替换为AuthorizationChecker的原因是,在这种情况下仅使用IsGrated方法。如果您的案例需要,您可以将其替换为TokenStorage,或者同时使用AuthorizationChecker和TokenStorage。问题是,我刚刚分叉并部署了,所以我不知道不推荐的类在哪里使用。此外,尽管这只是一个警告,但它完全破坏了我的new relic monitoring:这就是我想摆脱它的原因。所以,这完全正常,Symfony中有一些不推荐使用的代码。你可以试试symfony2.7或者在newrelic下加上一个忽略。你说的“试试symfony2.7”是什么意思?symfony标准回购协议已经基于symfony 2.7,不是吗?但我还是收到了这些不推荐的警告。是否可以配置Symfony来避免这些问题?您可以将php.ini配置为忽略它们禁用警告:我有同样的问题,这是由FOSUserBundle引起的,FOSUserBundle仍在使用不推荐的调用,即使在2.0dev版本中似乎也没有更新。感谢您非常清楚的回答。我也喜欢不推荐的警告,问题是他们破坏了我的newRelic监控。Flu您能告诉我如何在新类中使用注释安全系统吗?@Abdel5我认为最好自己提出一个问题,因为这里的问题仅仅是为了消除OP遇到的错误。谢谢您@Flu。你救了我几个小时!我喜欢这个答案,谢谢 public function getAuthorizationUrl(Request $request, $name, $redirectUrl = null, array $extraParameters = array()) { $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)); } else { $redirectUrl = $this->getServiceAuthUrl($request, $resourceOwner); } } return $resourceOwner->getAuthorizationUrl($redirectUrl, $extraParameters); }