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

Ala*_*ira 7 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-开始)但当我尝试运行该应用程序时,我收到此错误消息:

[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"
Run Code Online (Sandbox Code Playgroud)

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

有人能给我一些提示我做错了吗?

Ala*_*ira 11

我找到了解决方案!

首先,在security.conf中我们需要在防火墙之前添加登录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 }
Run Code Online (Sandbox Code Playgroud)

然后路由的配置需要在routes.yaml中定义,而不是在routing.yaml中定义

# config/routes.yaml
api_login_check:
    path: /api/login_check
Run Code Online (Sandbox Code Playgroud)

最后,我们需要删除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
Run Code Online (Sandbox Code Playgroud)

它应该做的工作!

我希望它可以帮助其他人解决同样的问题