错误:无法找到路径控制器“/ login_check”。路线错误地配置。 (LexikJWTAuthentication)

问题描述 投票:10回答:2

描述

当我正在做的事情http://localhost:8000/api/admin/login_check返回所需的JWT令牌运行正常请求我的symfony的服务器。

然而,当我与功能测试做(与./bin/phpunit)我得到以下错误:

错误:无法找到路径\“/ API /管理/ login_check \”的控制器。路线错误地配置。

我还通过functional test docs去了。

Bug Reproduced

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

我也能够通过克隆由lexikjwtauthenticationbundle的创造者之一提供了一个工作示例reproduce the bug

日志

Test Logs (error)

运行./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. [] []

Dev Logs (success)

做一个卷曲或邮递员请求时发生

[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} []

相关的代码:

Test method:

    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());

    }

Routes:

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

Security:

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 }

Question

为什么会出现功能性测试期间404 route not found路线/api/admin/login_check但工作正常,卷曲和邮递员?

Github #610

php phpunit symfony4 functional-testing lexikjwtauthbundle
2个回答
3
投票

您在您的测试要求qazxsw POI头错名字。它应该有名字qazxsw POI。

content-type

检查是在这里:供应商/ symfony中/安全/ HTTP /防火墙/ UsernamePasswordJsonAuthenticationListener.php ::处理

CONTENT_TYPE

它可以帮助你“的错误转载”至少代码。


2
投票

确保$client->request( 'POST', '/login_check', ['_username' => 'lexik', '_password' => 'dummy'], [], ['CONTENT_TYPE' => 'application/json'] ); 是防火墙之后if (false === strpos($request->getRequestFormat(), 'json') && false === strpos($request->getContentType(), 'json') ) { return; } 路径具有匹配防火墙模式。改变这种

访问控制:

- { path: ^/your-route-for-login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

访问控制:

- { path: ^/your-route-for-login$, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

我想:P


推荐问答