【问题标题】:FOSOAuthServerBundle vs Symfony3 Security howtoFOS OAuth Server Bundle vs Symfony3 Security 如何
【发布时间】:2016-12-07 09:16:58
【问题描述】:

我已经成功地使用 Symfony2.8 实现了 FOSOAuthServerBundle 并且它工作正常。 当我尝试使用 Symfony3.2 时出现错误:

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

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

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

非常感谢您的回答

【问题讨论】:

    标签: symfony fosoauthserverbundle


    【解决方案1】:

    不详细了解 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\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,
            ));
        }
    }
    

    【讨论】:

    • 是的,你是对的,只是不知道该怎么做。无论如何,非常感谢。
    【解决方案2】:

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

    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
        ));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2019-03-04
      • 2015-12-09
      • 1970-01-01
      • 2013-11-29
      • 2020-05-15
      相关资源
      最近更新 更多