Jwt Symfony4:找不到路径“的控制器”/api/登录检查;。路由配置错误

Jwt Symfony4:找不到路径“的控制器”/api/登录检查;。路由配置错误,jwt,symfony4,api-platform.com,lexikjwtauthbundle,Jwt,Symfony4,Api Platform.com,Lexikjwtauthbundle,我正在通过JWT设置symfony4 api JSON登录。 api平台核心包已安装,我遵循以下说明: 我按照描述创建了自定义用户提供程序。 通过打开URL/api/login\u check,出现错误消息“找不到路径/api/login\u check的控制器”。路由配置错误 通过发送POST请求,我得到了html格式的错误页面 这是我的路线。yaml: #index: # path: / # controller: App\Controller\DefaultController

我正在通过JWT设置symfony4 api JSON登录。 api平台核心包已安装,我遵循以下说明:

我按照描述创建了自定义用户提供程序。 通过打开URL/api/login\u check,出现错误消息“找不到路径/api/login\u check的控制器”。路由配置错误

通过发送POST请求,我得到了html格式的错误页面

这是我的路线。yaml

#index:
#    path: /
#    controller: App\Controller\DefaultController::index
api_login_check:
    path: /api/login_check
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 }
这是我的安全措施。yaml

#index:
#    path: /
#    controller: App\Controller\DefaultController::index
api_login_check:
    path: /api/login_check
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调试:路由返回:

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

有人知道我的错误是什么吗?

嗯,我想你不必重新定义路径:

api_login_check:
    path: /api/login_check
拆下它并测试它

并检查路径:/api/login\u check是否正确,因为这不是FOSUserBundle的标准登录检查


希望这有帮助。

使用路由调试器查看路由是否在此处:

php bin/console debug:route | grep login_check
如果没有,请添加它

内部路由.yml:

api_login_check:
    path: /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 }
路线。yaml

app:
  path: /{params}
  controller: App\Controller\DefaultController::index
  requirements:
       params: "^(?!admin|api).+"

api_login_check:
   path: /api/login
   methods: [POST]

似乎您尚未为此路径声明
控制器

api_login_check:
    path: /api/login_check
    controller: App\Controller\AuthenticationController::login

在security.yaml中注释这些行:

main:
anonymous: true

我遇到了同一个问题,似乎标题

    Content-Type: application/json

当您请求
/api/login\u check
我的安全性时必须添加。yaml与您的类似,您可能会错误地测试配置结果,重置url、用户名和密码

测试这一点:

curl -X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"username":"USER", "password":"PASSWORD"}
这是我的安全:

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

# 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: username
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

    api:
        pattern:   ^/api
        stateless: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator

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

当我删除它时,我会收到以下错误消息:“找不到“get/api/login_check”的路由,并且我不使用FOSUserBundle。我创建了一个自定义用户提供程序,如给出的url中所述。@user3684098您解决了这个问题吗?我也有同样的问题,我找到了解决办法
“Content Type:application/json”
标题应该被添加保存我的一天,花了3个小时与此没有人有想法吗((你最终解决了吗?这里也有同样的问题。是的,非常感谢。这解决了错误。购买为什么是这个要求。这个要求是来自Symfony 4还是JWT令牌包?我认为这是Symfony要求的-json_登录设置的文档说:。因此,如果你不指定头,请求将我不会被安全系统截获。我不能代表原始海报说话,但当我这么做时,路由显示出来。尽管如此,我仍然收到消息“找不到用于路径/api/login\u检查的控制器。路由配置错误”当尝试使用邮递员访问路由时。似乎一个好的解决方法是显式地将控制器设置为在routing.yml或route.yml中命中。我只是不知道要使用哪个控制器。这为我解决了这个问题!