【发布时间】:2021-11-16 05:32:36
【问题描述】:
我在 Laravel 应用程序中为 CORS 使用 \Fruitcake\Cors\HandleCors 中间件。
我的cors.php 文件如下所示:
'paths' => ['api/*', 'oauth/*', '*'],
'allowed_methods' => ['POST', 'GET', 'DELETE', 'PUT', '*'],
'allowed_origins' => ['http://localhost:3000', 'https://localhost:3000'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
我正在设置 cookie 作为响应:
$access_cookie = Cookie::make('access_token', $access_token, $cookieLife, null, null, true);
$refresh_cookie = Cookie::make('refresh_token', $refresh_token, $MINUTES_IN_ONE_YEAR, null, null, true);
return response()
->json(['tokens' => ['access_token' => $access_token, 'refresh_token' => $refresh_token], 'user' => $user])->withCookie($access_cookie)->withCookie($refresh_cookie);
最后,我从 https://localhost:3000 上运行的 React 应用程序调用这个 api 端点
它给了我一个错误,说 CORS 不允许(经典 CORS 错误)。
但是当我从响应中删除 cookie 时,它工作正常。
我还需要什么其他设置才能让它工作?
【问题讨论】:
-
这能回答你的问题吗? Set cookies for cross origin requests
-
不幸的是,这些答案对我没有帮助。
标签: php laravel cors laravel-8