11

描述

当我对运行在其上的 symfony 服务器发出正常请求时,http://localhost:8000/api/admin/login_check它会返回所需的 jwt 令牌。

但是,当我使用功能测试(使用./bin/phpunit)执行此操作时,出现以下错误:

错误:找不到路径 \"/api/admin/login_check\" 的控制器。路由配置错误。

我还浏览了功能测试文档

错误重现

不要犹豫,克隆或分叉这个项目进行测试。有 README.md 解释安装步骤。

通过克隆 lexikjwtauthenticationbundle 的创建者之一提供的工作示例,我还能够重现该错误。

日志

测试日志(错误)

运行时出现./bin/phpunit

[2019-01-31 09:37:49] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"http://localhost/api/admin/login_check","method":"POST"} []
[2019-01-31 09:37:49] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-01-31 09:37:49] request.WARNING: Unable to look for the controller as the "_controller" parameter is missing. [] []

开发日志(成功)

在执行 curl 或 postman 请求时发生

[2019-01-29 21:16:26] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"https://localhost:8000/api/admin/login_check","method":"POST"} []
[2019-01-29 21:16:27] doctrine.DEBUG: SELECT t0.id AS id_1, t0.email AS email_2, t0.password AS password_3 FROM admin t0 WHERE t0.email = ? LIMIT 1 ["email@test.com"] []
[2019-01-29 21:16:27] security.INFO: User has been authenticated successfully. {"username":null} []

相关代码:

测试方法:

    public function testLogin(){

        $client = static::createClient();
        $client->request('POST', '/api/admin/login_check', [], [],
            [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ],
            json_encode([
                'email' => 'email@test.com',
                'password' => 'qwerty123'
            ])
        );

        $this->assertEquals(200, $client->getResponse()->getStatusCode());

    }

路线:

# Admin Routes
api_admin_login_check:
    path: /api/admin/login_check
    methods:  [POST]

安全:

security:

# more configs here

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

        login_admin:
            pattern: ^/api/admin/login
            stateless: true
            anonymous: true
            json_login:
                username_path: email
                provider: app_admin_provider
                check_path: /api/admin/login_check
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure

        admin_api:
            pattern: ^/api/admin
            stateless: true
            provider: app_admin_provider
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

    access_control:
        - { path: ^/api/admin/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/admin, roles: ROLE_ADMIN }
        - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }

问题

为什么在功能测试期间有一条404 route not foundfor/api/admin/login_check路线,但在 curl 和 postman 中运行良好?

Github #610

4

3 回答 3

4

content-type您的测试请求中的标头名称错误。它应该有名字CONTENT_TYPE

$client->request(
   'POST',
   '/login_check',
   ['_username' => 'lexik', '_password' => 'dummy'],
   [],
   ['CONTENT_TYPE' => 'application/json']
);

检查在这里:供应商/symfony/security/Http/Firewall/UsernamePasswordJsonAuthenticationListener.php::handle

if (false === strpos($request->getRequestFormat(), 'json')
   && false === strpos($request->getContentType(), 'json')
) {
  return;
}

它至少有助于您的“重现错误”代码。

于 2019-02-12T04:05:35.180 回答
2

确保/login_check位于防火墙后面的 /login_check路径必须与防火墙模式匹配。 改变这个

访问控制:

- { 路径:^/your-route-for-login,角色:IS_AUTHENTICATED_ANONYMOUSLY,requires_channel:https }

访问控制:

- { 路径:^/your-route-for-login$,角色:IS_AUTHENTICATED_ANONYMOUSLY,requires_channel:https }

我试过了:P

于 2019-02-05T11:34:48.273 回答
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:53:24.120 回答