LexikJWTAuthenticationBundle - 没有能够为 "api_login_check" 加载配置的扩展

LexikJWTAuthenticationBundle - There is no extension able to load the configuration for "api_login_check"

我正在尝试使用 JWT 和 PHP 进行一些实验,但我无法使 LexikJWTAuthenticationBundle 工作。

我使用 composer 创建了一个 Symfony 项目 composer create-project symfony/skeleton my_project 并使用 Symfony Flex 安装了 LexikJWTAuthenticationBundle composer req jwt-auth 然后我按照 Github (https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#getting-started) 中的项目入门,但是当我尝试 运行 应用程序时,我收到此错误消息:

[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "api_login_check" (in /home/alan/Desktop/auth/config/packages/routing.yaml). Looked for namespace "api_login_check", found "framework", "security", "lexik_jwt_authentication" in /home/alan/Desktop/auth/config/packages/routing.yaml (which is loaded in resource "/home/alan/Desktop/auth/config/packages/routing.yaml").

[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for "api_login_check" (in /home/alan/Desktop/auth/config/packages/routing.yaml). Looked for namespace "api_login_check", found "framework", "security", "lexik_jwt_authentication"

我在 github 中使用出现错误的代码创建了一个存储库 https://github.com/alanoliveira/jwt_auth_test

有人可以告诉我我做错了什么吗?

您是否在 routing.yml 文件中添加了这条路线? :

api_login_check:
path: /api/login_check

我找到了解决方案!

首先,在security.conf中我们需要在[=25之前添加登录api防火墙=]主要防火墙。

# config/packages/security.yaml
security:
    # https://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: ~ }
    firewalls:
        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path:               /api/login_check
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

        api:
            pattern:   ^/api
            stateless: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: ~

            # activate different ways to authenticate

            # http_basic: ~
            # https://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            # https://symfony.com/doc/current/cookbook/security/form_login_setup.html

    access_control:
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

那么route的配置需要定义在routes.yaml,而不是routing.yaml

# config/routes.yaml
api_login_check:
    path: /api/login_check

最后我们需要删除framework.yaml

中会话行的注释
# config/packages/framework.yaml
framework:
    secret: '%env(APP_SECRET)%'
    #default_locale: en
    #csrf_protection: ~
    #http_method_override: true
    #trusted_hosts: ~
    # https://symfony.com/doc/current/reference/configuration/framework.html#handler-id
    session:
       # The native PHP session handler will be used
       handler_id: ~
    #esi: ~
    #fragments: ~
    php_errors:
        log: true

它应该能胜任!

希望对遇到同样问题的人有所帮助