Symfony 6.2 api 响应返回空数据

问题描述 投票:0回答:0

我有 API 请求的自定义身份验证器,当我尝试执行任何请求时,我得到的响应是空数据。

security.yaml

security:
    # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
    # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
        # used to reload user from session & other features (e.g. switch_user)

    enable_authenticator_manager: true

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        api:
            lazy: true
            pattern: ^/api/
            provider: app_user_provider
            custom_authenticators:
                - App\Security\MyApiAuthenticator

MyApiAuthenticator

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        // debug just help me trace in log file
        $this->logger->debug(__CLASS__.'::'.__FUNCTION__, ['$request' => $request, 'token' => $token, '$firewallName' => $firewallName]);
        
        // nothing to do
        return new Response(
            json_encode(['message' => 'hello world']),
            200,
            ['Content-Type' => 'application/json;charset=UTF-8']
        );
    }

有趣的是,如果您从标头中删除 Content-Type,响应 Content-Type 将更改为 text/html。

回应

HTTP/1.1 200 OK
Host: 127.0.0.1:41493
Date: Wed, 03 May 2023 08:16:24 GMT
Connection: close
X-Powered-By: PHP/8.1.2-1ubuntu2.11
Content-Type: application/json;charset=UTF-8
Cache-Control: max-age=0, must-revalidate, private
Date: Wed, 03 May 2023 08:16:24 GMT
X-Debug-Token: 1f3805
X-Debug-Token-Link: http://127.0.0.1:41493/_profiler/1f3805
X-Robots-Tag: noindex
Expires: Wed, 03 May 2023 08:16:24 GMT

PS:仅针对文件传输请求进行测试

api symfony response httpresponse
© www.soinside.com 2019 - 2024. All rights reserved.