0

我已经成功地使用 Symfony2.8 实现了 FOSOAuthServerBundle 并且它有效。当我尝试使用 Symfony3.2 时,我遇到了错误:

试图从命名空间“Symfony\Component\Security\Core”加载类“SecurityContext”。您是否忘记了另一个名称空间的“使用”语句?

所以我用谷歌搜索,现在知道 Symofny 3.2 中不再存在 SecurityContext 了。但是在 FOSOAuthServerBundle 官方文档“A Note About Security”中仍然存在函数 loginAction() 只能与 symfony2 压缩。

问题是: - 我可以将此捆绑包与 Symfony 3.2 一起使用吗?- 如果是,是否有任何文档如何做到这一点,或者更好的例子?

非常感谢您的回答

4

2 回答 2

3

不详细了解 FOSOAuthServerBundle。但我猜a_note_about_security的捆绑包文档中的示例已经过时了。

自 symfony 2.6 起,security.context 服务已被弃用。在这里您可以找到更改的说明:symfony.com/blog/new-in-symfony-2-6-security-component-improvements

您可以尝试替换\Symfony\Component\Security\Core\SecurityContext\Symfony\Component\Security\Core\Security

<?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-09T15:01:25.407 回答
0

我想到了。也许它可以帮助某人。

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
    ));
}
于 2016-12-10T18:41:20.127 回答