Symfony 6 请求标头获取授权空值

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

请提供一些帮助。我现在陷入困境,没有想法。 我正在开发一个 Angular 项目(前端),该项目向 Symfony 6 项目(后端)发出 API 请求。 在我的本地环境中一切正常,但我将其发布到托管,但遇到了问题。

服务器上的项目部分运行。一些 get 请求正在收集结果。问题出现在受保护的页面上。

从角度前端,我尝试通过对 Symfony 进行 POST 调用来创建一个实体。在此通话中,我添加了标题:

Content-Type: application/x-www-form-urlencoded
Authorization: [token string]

响应返回 500 错误,因为 Symfony 控制器中用于检索令牌值的行获取了空值。 这是控制器中的行:

#[Route('/category/create', name: 'app_category_create', methods:['POST'])]
    public function create(Request $request, ManagerRegistry $doctrine, JwtAuth $jwt_auth)
    {
        $token = $request->headers->get('Authorization', null);
...

我已经在 .htaccess 中找到了这个:

DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    Options +SymLinksIfOwnerMatch
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
    </IfModule>
</IfModule>

最后这是index.php

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials:true");
header("Access-Control-Allow-Headers: Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");

正如我在开始时提到的,它在我的本地运行正常。我不明白发生了什么......

有人可以帮我弄清楚为什么后端没有收到令牌值吗?

编辑 我更改了后端index.php中的一些标头,但授权标头仍然为空值。

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials:true");
header("Access-Control-Allow-Headers: Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, x-auth-token");
header("Access-Control-Expose-Headers: *"); 
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");

我尝试使用 X-API-KEY 标头发送相同的令牌,并且它有效。我很迷失。我不明白发生了什么......

angular symfony request http-headers
1个回答
0
投票

我在这里遇到同样的问题。它在我的本地环境中正常工作,但在我的服务器上却无法正常工作。我尝试用Postman测试它并遇到同样的错误,让我相信问题出在后端。你找到解决办法了吗?

© www.soinside.com 2019 - 2024. All rights reserved.