API 平台 JWT:未找到“GET/api/login"的路由; [英] API-Platform JWT : No route found for "GET /api/login"

查看:36
本文介绍了API 平台 JWT:未找到“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:52request 无法查找控制器,因为缺少_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

{"username": "john","password": "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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆