JWTでsymfony4 api JSONログインを設定する必要があります。 APIプラットフォームコアバンドルがインストールされており、次の指示に従いました: https://api-platform.com/docs/core/jwt/
説明どおりにカスタムユーザープロバイダーを作成しました。 URL/api/login_checkを開くと、エラーメッセージ「パス "/ api/login_check"のコントローラーが見つかりません。ルートが正しく構成されていません。」発生します。
POSTリクエストを送信することで、エラーページがHTMLで表示されます。
これは私のroutes.yamlです:
#index:
# path: /
# controller: App\Controller\DefaultController::index
api_login_check:
path: /api/login_check
そして、これが私の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 }
bin/console debug:routeは次を返します:
--------------------------- -------- -------- ------ -------------------------------------
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
--------------------------- -------- -------- ------ -------------------------------------
誰かが私の間違いを知っていますか?
同じ問題がありましたが、ファイアウォールでネストされた構成の順序を変更すると、その問題が解決され、lexik jwtが機能し始めました。私の設定:
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]
このパスのController
を宣言していないようです:
api_login_check:
path: /api/login_check
controller: App\Controller\AuthenticationController::login
ルートデバッガーを使用して、ルートがここにあるかどうかを確認します。
php bin/console debug:route | grep login_check
そうでない場合は、追加してください!
Route.yml内:
api_login_check:
path: /api/login_check
(または、追加するバンドルの構成を確認してください)。
まあ、私はあなたがパスを再定義する必要はないと思います:
api_login_check:
path: /api/login_check
それを削除してテストします。
これがpath: /api/login_check
は正解です。これは、FOSUserBundleの標準のlogin_checkではないためです。
お役に立てれば。