7

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

我使用 composer 创建了一个 Symfony 项目composer create-project symfony/skeleton my_project并使用 Symfony Flex 安装 LexikJWTAuthenticationBundlecomposer 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"

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

有人可以给我一些提示我做错了什么吗?

4

2 回答 2

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 }

那么路由的配置需要在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

它应该做的工作!

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

于 2017-11-16T21:23:45.873 回答
0

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

api_login_check:
path: /api/login_check
于 2017-11-09T14:20:25.290 回答