在 iframe 中嵌入 Laravel 应用程序,登录时出现 302 错误
Embedded Laravel App In Iframe, Give 302 Error On Login
我正在尝试在 iframe
.
的其他域中使用 Laravel app
我写了一个 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 app
需要来自 iframe 的身份验证。它使用的是表单提交,而不是 API。然后它开始给出一个
419 error
。我通过在 VerifyCsrfToken
中间件 $except
数组中添加一个登录路由来解决 csrf
错误。
然后我检查 network
和 post
登录请求给出 302 错误
我测试了身份验证方法,经过身份验证的用户 object 返回 Dashboard
页面重定向,然后 302 error code
显示。
此外,我在 config/session.php
中设置了 same_site => null
,但仍然不起作用。
如@apokryfos所说,我需要将cookie
设置为secure
并设置SameSite=None
.
转到:config/session.php
设置:
'same_site' => 'none'
和 'secure' => env('SESSION_SECURE_COOKIE', true)
或 通过在 .env
文件中设置 SESSION_SECURE_COOKIE = true
。
并且不要忘记创建 FrameMiddleware
,因为您可以 和 white_labeled
域,即您嵌入 iframe 的 http://localhost。
注意: 你可以验证 cookies are secured or not
和 SameSite=Null
通过使用浏览器开发工具,如下图所示:
我正在尝试在 iframe
.
Laravel app
我写了一个 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 app
需要来自 iframe 的身份验证。它使用的是表单提交,而不是 API。然后它开始给出一个419 error
。我通过在 VerifyCsrfToken
中间件 $except
数组中添加一个登录路由来解决 csrf
错误。
然后我检查 network
和 post
登录请求给出 302 错误
我测试了身份验证方法,经过身份验证的用户 object 返回 Dashboard
页面重定向,然后 302 error code
显示。
此外,我在 config/session.php
中设置了 same_site => null
,但仍然不起作用。
如@apokryfos所说,我需要将cookie
设置为secure
并设置SameSite=None
.
转到:config/session.php
设置:
'same_site' => 'none'
和 'secure' => env('SESSION_SECURE_COOKIE', true)
或 通过在 .env
文件中设置 SESSION_SECURE_COOKIE = true
。
并且不要忘记创建 FrameMiddleware
,因为您可以 white_labeled
域,即您嵌入 iframe 的 http://localhost。
注意: 你可以验证 cookies are secured or not
和 SameSite=Null
通过使用浏览器开发工具,如下图所示: