gpt4 book ai didi

php - 错误 : Unable to find the controller for path "/login_check". 路由配置错误。 (LexikJWTAuthentication)

转载 作者:可可西里 更新时间:2023-10-31 22:49:48 27 4
gpt4 key购买 nike

描述

当我对在 http://localhost:8000/api/admin/login_check 上运行的 symfony 服务器执行正常请求时,它返回所需的 jwt token .

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

Error: Unable to find the controller for path \"/api/admin/login_check\". The route is wrongly configured.

我还浏览了 functional test docs .

Bug Reproduced

不要犹豫,克隆或 fork 此项目进行测试。 README.md 解释了安装步骤。

我也能够 reproduce the bug通过克隆 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 }

问题

为什么在功能测试期间 /api/admin/login_check 路由有一个 404 route not found 但在 curl 和 postman 上工作正常?

Github #610

最佳答案

您的测试请求中的 content-type header 名称有误。它的名称应为 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;
}

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

关于php - 错误 : Unable to find the controller for path "/login_check". 路由配置错误。 (LexikJWTAuthentication),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54407480/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com