我已经用Symfony2.8成功地实现了FOSOAuthServerBundle,并且它起作用了。当我尝试使用Symfony3.2时,我遇到了错误:
尝试从命名空间"Symfony\Component\Security\Core“加载类"SecurityContext”。您是否忘记了另一个名称空间的"use“语句?
所以我谷歌了一下,现在知道SecurityContext在Symofny3.2中已经不存在了。但在FOSOAuthServerBundle官方文档“关于安全性的说明”中,仍然存在只能与symfony2压缩的函数loginAction()。
问题是:-我可以在Symfony 3.2中使用这个捆绑包吗?-如果可以,有没有任何文档可以做到这一点,或者更好的例子?
非常感谢您的回答
发布于 2016-12-09 23:01:25
不知道详细的FOSOAuthServerBundle。但我猜a_note_about_security的捆绑包文档中的示例已经过时了。
security.context服务从symfony 2.6开始就被弃用了。你可以在这里找到这些变化的描述:symfony.com/blog/new-in-symfony-2-6-security-component-improvements
您可以尝试用\Symfony\Component\Security\Core\Security替换\Symfony\Component\Security\Core\SecurityContext
<?php
// src/Acme/SecurityBundle/Controller/SecurityController.php
namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Security;
class SecurityController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
} else {
$error = $session->get(Security::AUTHENTICATION_ERROR);
$session->remove(Security::AUTHENTICATION_ERROR);
}
// Add the following lines
if ($session->has('_security.target_path')) {
if (false !== strpos($session->get('_security.target_path'), $this->generateUrl('fos_oauth_server_authorize'))) {
$session->set('_fos_oauth_server.ensure_logout', true);
}
}
return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(Security::LAST_USERNAME),
'error' => $error,
));
}
}发布于 2016-12-11 02:41:20
我想通了。也许它能帮到别人。
public function loginAction(Request $request) {
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('AppBundle:Security:login.html.twig', array(
'last_username' => $lastUsername
, 'error' => $error
));
}https://stackoverflow.com/questions/41013496
复制相似问题