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

标签 php symfony jwt lexikjwtauthbundle

我正在尝试使用 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

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

最佳答案

我找到了解决方案!

首先,在security.conf中我们需要在ma​​in防火墙之前添加loginapi防火墙。

# 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

中 session 行的注释
# 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

它应该能胜任!

希望能帮到遇到同样问题的人

关于php - LexikJWTAuthenticationBundle - 没有扩展能够加载 "api_login_check"的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47204064/

相关文章:

php - 将网站字符编码从 iso-8859-1 更改为 UTF-8

php - 从服务内部调用服务?

用于更改数组的 "order"的 PHP 算法

javascript - REST API和JWT身份验证: send data along token?

php - PHP 和 Ajax 的进度条

继承

php - 无法在 PHP 中连接 2 个数组

php - 我如何在 symfony2 中使用 DQL 获得单个结果

javascript - Mobx 强制更新计算值

使用 ExtraScopesToConsent 和 AcquireTokenSilent 对多个受众进行 Azure 身份验证