首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FOSOAuthServerBundle vs Symfony3安全操作指南

FOSOAuthServerBundle vs Symfony3安全操作指南
EN

Stack Overflow用户
提问于 2016-12-07 17:16:59
回答 2查看 693关注 0票数 0

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

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

所以我谷歌了一下,现在知道SecurityContext在Symofny3.2中已经不存在了。但在FOSOAuthServerBundle官方文档“关于安全性的说明”中,仍然存在只能与symfony2压缩的函数loginAction()。

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

非常感谢您的回答

EN

回答 2

Stack Overflow用户

发布于 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

代码语言:javascript
复制
<?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,
        ));
    }
}
票数 3
EN

Stack Overflow用户

发布于 2016-12-11 02:41:20

我想通了。也许它能帮到别人。

代码语言:javascript
复制
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
    ));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41013496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档