我正在尝试使用 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中我们需要在main防火墙之前添加login和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
中 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/