API平台JWT:找不到"GET/api/login"的路由; [英] API-Platform JWT : No route found for "GET /api/login"
问题描述
我成功安装了API平台,它可以与我的所有实体很好地配合使用. 现在,我尝试添加带有LexikJWTAuthenticationBundle的JWT身份验证,但是当我发送登录请求时,我得到了:
I successfully installed API Platform, it works well with all my entities. Now i'm trying to add JWT authentication whith LexikJWTAuthenticationBundle, but when i send the request for login i get :
找不到"GET/api/login"的路由
No route found for "GET /api/login"
我的请求:
http://localhost:8000/api/login?username = john& ; password = doe
我正在使用Symfony 4,这是我的 security.yaml :
I'm using Symfony 4, here is my security.yaml :
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
entity_provider:
entity:
class: App\Entity\User
property: username
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
provider: entity_provider
json_login:
check_path: /api/login
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
provider: entity_provider
stateless: true
anonymous: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
我认为JWT捆绑软件很好用,因为当我尝试访问资源时,我会得到:
I think the JWT bundle works well because when i try to access a resource, i get :
{"code":401,"message":"JWT Token not found"}
我认为这只是路由问题,但是由于我是Symfony的新手,所以我不知道该怎么办...
I think it's just a matter of routing, but as i'm quite a newbie to Symfony i don't know what to do...
我已经尝试过更改模式,检查路径...
I already tried to change patterns, check path...
有任何提示吗?
编辑:我在routes.yaml中添加了此内容:
EDIT : i added this in routes.yaml :
api_login_check:
path: /api/login
现在我有:
无法找到路径"/api/login"的控制器.路由配置错误.
Unable to find the controller for path "/api/login". The route is wrongly configured.
日志中的更多详细信息:
More details from the logs :
警告 09:40:52 请求由于缺少"_controller"参数而无法找到控制器.
WARNING 09:40:52 request Unable to look for the controller as the "_controller" parameter is missing.
错误 09:40:53 请求未捕获的PHP异常Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:找不到路径"/api/login"的控制器.路由配置错误."
ERROR 09:40:53 request Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "Unable to find the controller for path "/api/login". The route is wrongly configured."
推荐答案
检查此链接 https://github.com/symfony/symfony-docs/pull/7081/files#diff-7f5c7908922a550bda01ab86f19f3938R119
您必须使用以下命令将请求发送到 http://localhost:8000/api/login 这样的json正文
You have to send your request to http://localhost:8000/api/login with a json body like this
{用户名":"john",密码":"doe"}
{"username": "john","password": "doe"}
我还建议阅读此如何构建JSON身份验证端点" https: //symfony.com/doc/current/security/json_login_setup.html
Also I recommend to read this "How to Build a JSON Authentication Endpoint" https://symfony.com/doc/current/security/json_login_setup.html
谢谢
这篇关于API平台JWT:找不到"GET/api/login"的路由;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!