我正在尝试在 iframe
中的其他域中使用 Laravel 应用程序
。
我写了一个 FrameMiddleware
,在其中我允许在 header 中使用该域
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$response->header('Content-Security-Policy', 'frame-ancestors http://localhost');
return $response;
}
并通过使用上述中间件 cors 消除了原始错误。
因为我的 Laravel 应用程序
需要来自 iframe 的身份验证。它使用的是表单提交,而不是 API。然后它开始给出一个
419 错误
。我通过在 VerifyCsrfToken
中间件 $except
数组中添加一个登录路由来解决 csrf
错误。
然后我检查 network
和 post
登录请求给出 302 错误
我测试了身份验证方法,经过身份验证的用户对象通过 Dashboard
页面重定向返回,然后显示 302 错误代码
。
此外,我在 config/session.php
中设置了 same_site => null
但仍然无法正常工作。
最佳答案
作为@apokryfos说,我需要将 cookie
设置为 secure
并设置 SameSite=None
。
转到:config/session.php
设置:
'same_site' => 'none'
和 'secure' => env('SESSION_SECURE_COOKIE', true)
OR 通过设置 SESSION_SECURE_COOKIE = true
在 .env
文件中。
不要忘记尽可能地创建FrameMiddleware
follow-my-question-thread和 white_labeled
域,即您嵌入 iframe 的 http://localhost。
注意:您可以验证 cookie 是否安全
以及 SameSite=Null
使用浏览器开发工具,如下图所示:
关于php - 在 Iframe 中嵌入 Laravel 应用程序,登录时出现 302 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69374643/