问题
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