Symfony - 使用 LexikJWTAuthenticationBundle 设置 TokenController

凯文

我正在使用 LexikJWTAuthenticationBundle https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation

我正在设置我的控制器以获取令牌:

class TokenController extends AbstractController
{
    /**
     * @Route("/api/token", name="token", methods={"POST"})
     * @param Request $request
     * @param JWTEncoderInterface $JWTEncoder
     * @return JsonResponse
     * @throws \Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException
     */
    public function token(Request $request, JWTEncoderInterface $JWTEncoder)
    {
        $user = $this->getDoctrine()->getRepository(User::class)->findOneBy([
            'email' => $request->getUser(),
        ]);

        if (!$user) {
            throw $this->createNotFoundException('User Not Found');
        }

        $isValid = $this->get('security.password_encoder')
            ->isPasswordValid($user, $request->getPassword());
        if (!$isValid) {
            throw new BadCredentialsException();
        }
        $token = $JWTEncoder->encode([
                'email' => $user->getEmail(),
                'exp' => time() + 3600 // 1 hour expiration
            ]);

        return new JsonResponse(['token' => $token]);
    }
}

但我有这个错误:

找不到服务“security.password_encoder”:即使它存在于应用程序的容器中,“App\Controller\TokenController”中的容器是一个较小的服务定位器,只知道“doctrine”、“form.factory”、“http_kernel” ", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" 和 "twig" 服务。除非您需要额外的懒惰,否则请尝试使用依赖注入。否则,您需要使用“TokenController::getSubscribedServices()”来声明它。

我使用了依赖注入,这是我的服务配置

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

问题出在哪儿?

伊万·维贾亚

AbstractController使用此控制器从 扩展,您使用的服务$this->get()将受到限制。要访问密码编码器服务,您可以注入Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface控制器操作或通过控制器类构造函数。

private $passwordEncoder;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
  $this->passwordEncoder = $passwordEncoder;
}
...

$this->passwordEncoder->isPasswordValid()
...

或者,您可以从扩展Symfony\Bundle\FrameworkBundle\Controller\Controller获取完整的容器访问权限。$this->get('security.password_encoder')应该与此合作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Symfony2中的内存提供程序设置显示名称

来自分类Dev

Symfony2-如何设置控制器createForm函数使用的实体管理器?

来自分类Dev

尝试使用在NGINX / HHVM上运行的Symfony设置Memcached

来自分类Dev

使用方案路由设置的Symfony2无限重定向循环

来自分类Dev

与FOSOAuthServerBundle TokenController一起使用“转发”

来自分类Dev

使用ID设置Symfony2 Entity表单字段

来自分类Dev

Symfony尝试在设置参数中使用ID数组

来自分类Dev

无法使用NetBeans IDE(8.1)设置Symfony2框架

来自分类Dev

如何使用symfony2在Twig中设置全局变量

来自分类Dev

LexikJWTAuthenticationBundle生成令牌

来自分类Dev

如何使用symfony / panther设置代理服务器

来自分类Dev

使用Symfony PHPUnit桥ClockMock设置日期/时间

来自分类Dev

LexikJWTAuthenticationBundle与多个提供程序

来自分类Dev

如何使用Doctrine和Symfony交易进行安全测试和设置?

来自分类Dev

Symfony2-Twig:使用twig变量设置下拉列表的值

来自分类Dev

Symfony 2使用CSS设置背景图像

来自分类Dev

使用Symfony2设置Payum Bundle出现错误

来自分类Dev

无法使用symfony项目设置Gitlab-ci

来自分类Dev

使用“定位”以Symfony形式设置默认选择

来自分类Dev

Symfony LexikJWTAuthenticationBundle无法验证

来自分类Dev

使用 FOSUserBundle 在 Symfony 中注册后设置 ROLE_USER

来自分类Dev

使用带有 twig symfony 的数据默认文件设置图像

来自分类Dev

Symfony Rest API lexikjwtauthenticationbundle JWT linux

来自分类Dev

在 AWS ElasticBeanstalk 上使用 Symfony 设置定期任务(使用 cron.yaml)

来自分类Dev

Symfony - 使用 Form Even Listener 设置数据

来自分类Dev

Symfony 4 API 平台+LexikJWTAuthenticationBundle:凭据错误

来自分类Dev

如何使用 symfony 3.4 在请求上设置语言环境

来自分类Dev

401/405 错误 Symfony 4 REST:docker 上的 JWT API 身份验证 (LexikJWTAuthenticationBundle) - ngnix

来自分类Dev

如何使用 MySQL 为 Symfony 项目设置 Docker?

Related 相关文章

  1. 1

    使用Symfony2中的内存提供程序设置显示名称

  2. 2

    Symfony2-如何设置控制器createForm函数使用的实体管理器?

  3. 3

    尝试使用在NGINX / HHVM上运行的Symfony设置Memcached

  4. 4

    使用方案路由设置的Symfony2无限重定向循环

  5. 5

    与FOSOAuthServerBundle TokenController一起使用“转发”

  6. 6

    使用ID设置Symfony2 Entity表单字段

  7. 7

    Symfony尝试在设置参数中使用ID数组

  8. 8

    无法使用NetBeans IDE(8.1)设置Symfony2框架

  9. 9

    如何使用symfony2在Twig中设置全局变量

  10. 10

    LexikJWTAuthenticationBundle生成令牌

  11. 11

    如何使用symfony / panther设置代理服务器

  12. 12

    使用Symfony PHPUnit桥ClockMock设置日期/时间

  13. 13

    LexikJWTAuthenticationBundle与多个提供程序

  14. 14

    如何使用Doctrine和Symfony交易进行安全测试和设置?

  15. 15

    Symfony2-Twig:使用twig变量设置下拉列表的值

  16. 16

    Symfony 2使用CSS设置背景图像

  17. 17

    使用Symfony2设置Payum Bundle出现错误

  18. 18

    无法使用symfony项目设置Gitlab-ci

  19. 19

    使用“定位”以Symfony形式设置默认选择

  20. 20

    Symfony LexikJWTAuthenticationBundle无法验证

  21. 21

    使用 FOSUserBundle 在 Symfony 中注册后设置 ROLE_USER

  22. 22

    使用带有 twig symfony 的数据默认文件设置图像

  23. 23

    Symfony Rest API lexikjwtauthenticationbundle JWT linux

  24. 24

    在 AWS ElasticBeanstalk 上使用 Symfony 设置定期任务(使用 cron.yaml)

  25. 25

    Symfony - 使用 Form Even Listener 设置数据

  26. 26

    Symfony 4 API 平台+LexikJWTAuthenticationBundle:凭据错误

  27. 27

    如何使用 symfony 3.4 在请求上设置语言环境

  28. 28

    401/405 错误 Symfony 4 REST:docker 上的 JWT API 身份验证 (LexikJWTAuthenticationBundle) - ngnix

  29. 29

    如何使用 MySQL 为 Symfony 项目设置 Docker?

热门标签

归档