在 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 错误。

然后我检查 networkpost 登录请求给出 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 notSameSite=Null 通过使用浏览器开发工具,如下图所示: