20

我很想通过 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                     
 --------------------------- -------- -------- ------ -------------------------------------

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

4

9 回答 9

15

我遇到了同样的问题,似乎标题

    Content-Type: application/json

请求时必须添加/api/login_check

于 2019-08-21T13:42:20.963 回答
6

在 security.yaml 中评论这些行:

main:
anonymous: true
于 2019-06-12T22:19:26.983 回答
3

有同样的问题,但是当我改变防火墙中嵌套配置的顺序时,它解决了这个问题,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]
于 2019-02-23T11:20:55.133 回答
1

似乎您还没有Controller为此路径声明 a :

api_login_check:
    path: /api/login_check
    controller: App\Controller\AuthenticationController::login
于 2019-05-22T10:40:31.947 回答
0

使用路由调试器查看路由是否在这里:

php bin/console debug:route | grep login_check

如果没有,请添加它!

内部 route.yml:

api_login_check:
    path: /api/login_check

(或检查应该为您添加它的捆绑包的配置)。

于 2018-03-09T10:44:50.837 回答
0

我的security.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 }
于 2020-06-18T21:00:08.520 回答
0

对我来说,我所要做的就是将登录和 api 防火墙放在主要防火墙之上:

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

login:
    pattern: ^/api/login
    stateless: true
    json_login:
        check_path: /api/login
        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
    stateless: true
    jwt: ~

main:
    form_login:
        # "login" is the name of the route created previously
        login_path: app_login
        check_path: app_login
        enable_csrf: true
        
于 2021-12-11T13:24:41.597 回答
0

好吧,我认为您不必重新定义路径:

api_login_check:
    path: /api/login_check

删除它并测试它。

并检查这 path: /api/login_check是否正确,因为这不是 FOSUserBundle 的标准 login_check。

希望这可以帮助。

于 2018-02-28T15:56:37.257 回答
0

经过几个不眠之夜后,以下场景对我有用。

我最初在连接 Postman 时遇到问题,单击标题并将其更改为 Application/JSON 是不够的,因为我不断收到 ORM/Doctrine 错误。对我有用的是我在我的一个 PHP 类中使用了不推荐使用的函数。

我只是去了我的日志文件 Var/log/dev.log 并发现了这个错误:

php.CRITICAL:未捕获的错误:无法将 Doctrine\ORM\PersistentCollection 分配给属性 Guess\Domain\Player\Player::$guesses 类型的 Doctrine\Common\Collections\ArrayCollection

进一步的研究表明,我使用了已弃用到 Collection 的 ArrayCollection 对象,因此将 ArrayCollection 更改为仅包含 getter 和 setter 方法的 Collection,然后重试。

这对我有用,我很高兴。

参考: Doctrine assignment error: Cannot assign Doctrine\ORM\PersistentCollection to property

这可能会在不久的将来帮助某人。

于 2021-11-04T19:54:26.207 回答