How to add Login to swagger UI with API PLATFORM (symfony 4)?

风格不统一 提交于 2021-02-11 13:48:00

问题


I installed & configured LexikJWTAuthenticationBundle, it works fine but I've got a small problem.

I have include the Authorization button for put the JWT token, the problem is the only way I can have my token is to use this commands:

curl -X POST -H "Content-Type: application/json" http://localhost:8000/api/login_check -d '{"username":"johndoe","password":"test"}'

It send my the token and I put it in the API, OK.

First problem: When I try this request with POSTMAN I get an error :

Unable to find the controller for path "/api/login_check". The route is wrongly configured. (404 Not Found)

The request : localhost:8000/api/login_check?username=johndoe&password=test with POST method

So with curl it's working but not with POSTMAN, why ? Here is my security.yaml:

security:
encoders:
    App\Entity\User:
        algorithm: argon2i

providers:
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        json_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

    register:
        pattern:  ^/api/register
        stateless: true
        anonymous: true

    api:
        pattern:  ^/api
        stateless: true
        anonymous: true
        provider: app_user_provider

    main:
        anonymous: true

access_control:
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY }

role_hierarchy:
    ROLE_DELEGATION:        [ROLE_USER]
    ROLE_EXPORT:            [ROLE_USER]
    ROLE_USER_ADMIN:        [ROLE_USER]
    ROLE_LIST_ADMIN:        [ROLE_USER]
    ROLE_IMPORT:            [ROLE_USER]
    ROLE_MOBILE:            [ROLE_USER]
    ROLE_ADMIN:             [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
    ROLE_SUPER_ADMIN:       [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]

Second problem, I'd like to add the login inside the Swagger. Like this:

I have no idea how to doing this, tryed to add @ApiRessource() inside the LoginCheckController but it's doing nothing.

Need some help / tips, thanks by advance


回答1:


The 404 message tells you there's either no route defining /api/login_check or there's a configuration problem with it and the route is not found You should check and debug your routes first and see if this is already defined

login_check:
path: /login_check

You might also want to check this GitHub Issue

Later Edit: After researching a bit more, I found a possible solution to your problem here on Stackoverflow




回答2:


Just put your firewall login at first place, in your example above the dev.



来源:https://stackoverflow.com/questions/54380730/how-to-add-login-to-swagger-ui-with-api-platform-symfony-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!