如何在Symfony 4中泛化ApiKeyAuthenticator?

时间:2019-06-08 11:20:24

标签: symfony symfony4

我有以下代码,用于在将数据发送到前端之前检查API密钥是否正确。

file1Controller.php


<?php


namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class file1Controller extends AbstractController
{

    /**
     * @Route("/Some/URI", methods={"GET"}) // "/Some/URI" here
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        if (empty($request->headers->get('api-key'))) {
            return new JsonResponse(['error' => 'Please provide an API_key'], 401);
        }

        if ($request->headers->get('api-key') !== $_ENV['API_KEY']) {
            return new JsonResponse(['error' => 'Invalid API key'], 401);
        }

        return new JsonResponse($this->getDoctrine()->getRepository('App:Something')->findAll()); //Something here
    }
}

在我的简单学习示例中,哪个工作如预期那样出色(已通过Postman进行了测试)。我想对此进行概括,以便可以在其他地方使用它。除了有注释的部分,几乎所有内容都应保持不变。我尝试了以下方法:

General.php

<?php


namespace App;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;


class General extends AbstractController
{

    private $request;
    private $route;
    private $entity;

    /**
     * ApiKeyAuthenticator constructor.
     * @param Request $request
     * @param String $route
     * @param String $entity
     */
    function __construct(Request $request, String $route, String $entity)
    {
        $this->request = $request;
        $this->route = $route;
        $this->entity = $entity;
    }

    /**
     * @Route({$route}, methods={"GET"}) //notice here
     * @return JsonResponse
     */
    public function list()
    {
        if (empty($this->request->headers->get('api-key'))) {
            return new JsonResponse(['error' => 'Please provide an API_key'], 401);
        }

        if ($this->request->headers->get('api-key') !== $_ENV['API_KEY']) {
            return new JsonResponse(['error' => 'Invalid API key'], 401);
        }

        return new JsonResponse($this->getDoctrine()->getRepository('App:{$this->entity}')->findAll()); //notice here
    }

}

然后将file1Controller.php的代码更改为:

<?php


namespace App\Controller;

require(__DIR__.'/../General.php'); //note that there's no error accessing the file here
use Symfony\Component\HttpFoundation\Request;

class file1Controller
{

    /**
     * @param Request $request
     */
    public function AuthenticateAPI(Request $request)
    {
        $AuthenticatorObject = new ApiKeyAuthenticator($request, "/Some/URI", 'Something'); //getting undefiend class 
        return $AuthenticatorObject;
    }

}

不幸的是,当用Postman测试它时,此方法不起作用,并且在$AuthenticatorObject = new ApiKeyAuthenticator($request, "/Some/URI", 'Something');的此行file1Controller.php上出现了未定义的类错误

我做错了什么,我该如何解决?

1 个答案:

答案 0 :(得分:2)

您不应在Symfony中这样称呼您的控制器:

require(__DIR__.'/../General.php'); //note that there's no error accessing the file here

请在Symfony文档中查看将控制器作为服务定义和访问:

How to Define Controllers as Services

How to Forward Requests to another Controller