Je suis en train de mettre en place une connexion JSON symfony4 api JSON. Le bundle de base de la plate-forme API est installé et j'ai suivi ces instructions: https://api-platform.com/docs/core/jwt/
J'ai créé le fournisseur d'utilisateurs personnalisé comme décrit. En ouvrant l'URL/api/login_check, le message d'erreur "Impossible de trouver le contrôleur pour le chemin"/api/login_check ". La route est mal configurée." se produit.
En envoyant une demande POST j'obtiens la page d'erreur en html.
Voici mes routes.yaml :
#index:
# path: /
# controller: App\Controller\DefaultController::index
api_login_check:
path: /api/login_check
Et voici mon security.yaml :
security:
encoders:
App\Security\User\WebserviceUser: bcrypt
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
webservice:
id: App\Security\User\WebserviceUserProvider
in_memory: { memory: ~ }
main:
entity: { class: App\Entity\User, property: email }
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
provider: webservice
json_login:
check_path: /api/login_check
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
provider: webservice
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: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
débogage bin/console: route renvoie:
--------------------------- -------- -------- ------ -------------------------------------
Name Method Scheme Host Path
--------------------------- -------- -------- ------ -------------------------------------
api_entrypoint ANY ANY ANY /api/{index}.{_format}
api_doc ANY ANY ANY /api/docs.{_format}
api_jsonld_context ANY ANY ANY /api/contexts/{shortName}.{_format}
api_users_get_collection GET ANY ANY /api/users.{_format}
api_users_post_collection POST ANY ANY /api/users.{_format}
api_users_get_item GET ANY ANY /api/users/{id}.{_format}
api_users_delete_item DELETE ANY ANY /api/users/{id}.{_format}
api_users_put_item PUT ANY ANY /api/users/{id}.{_format}
_twig_error_test ANY ANY ANY /_error/{code}.{_format}
api_login_check ANY ANY ANY /api/login_check
--------------------------- -------- -------- ------ -------------------------------------
Quelqu'un sait-il quelle est mon erreur?
J'ai eu le même problème, mais quand j'ai changé l'ordre de configuration imbriquée dans le pare-feu, cela a résolu ce problème et lexik jwt a commencé à fonctionner. ma config:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
# activate different ways to authenticate
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
check_path: /api/login
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
main:
anonymous: true
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /api/user/activate, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
routes.yaml
app:
path: /{params}
controller: App\Controller\DefaultController::index
requirements:
params: "^(?!admin|api).+"
api_login_check:
path: /api/login
methods: [POST]
Il semble que vous n'ayez pas déclaré de Controller
pour ce chemin:
api_login_check:
path: /api/login_check
controller: App\Controller\AuthenticationController::login
Utilisez le débogueur d'itinéraire pour voir si l'itinéraire est ici:
php bin/console debug:route | grep login_check
Sinon, ajoutez-le!
À l'intérieur de route.yml:
api_login_check:
path: /api/login_check
(ou vérifiez la configuration du bundle qui devrait l'ajouter pour vous).
Eh bien, je pense que vous n'avez pas à redéfinir le chemin:
api_login_check:
path: /api/login_check
retirez-le et testez-le.
et vérifiez si cela path: /api/login_check
est correct, car ce n'est pas le login_check standard de FOSUserBundle.
J'espère que cela t'aides.