在 Iframe 中嵌入 Laravel 应用程序,登录时出现 302 错误

M-K*_*war 4 php authentication iframe laravel csrf-token

Laravel app我正在尝试在 中的其他一些域中使用iframe

我写了一个FrameMiddleware,其中我在标头中允许该域

 public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        $response->header('Content-Security-Policy', 'frame-ancestors http://localhost');
        return $response;
    }

Run Code Online (Sandbox Code Playgroud)

并通过使用上面的中间件 cors origin 错误被删除。

因为我Laravel app需要来自 iframe 的身份验证。它使用表单提交,而不是 API。然后它开始给出一个
419 error. VerifyCsrfToken我通过在中间件数组中添加登录路由$except来排除csrf错误来解决。

然后我检查登录请求给出 302network错误post

iframe 中的 laravel 对于 post 请求给出 302 错误

我测试了身份验证方法,经过身份验证的用户对象通过页面重定向返回Dashboard,然后302 error code显示。

而且,我已经开始same_site => null工作了config/session.php,但仍然没有工作。

M-K*_*war 7

正如@apokryfos所说,我需要将 设置cookie为 besecure并设置SameSite=None

去 :config/session.php

设置: 'same_site' => 'none''secure' => env('SESSION_SECURE_COOKIE', true) 通过SESSION_SECURE_COOKIE = true在文件中设置.env

并且不要忘记创建,FrameMiddleware因为您可以关注我的问题线程white_labeled域,即您要在其中嵌入 iframe 的 http://localhost。

注意:您可以使用浏览器开发工具进行验证,cookies are secured or not如下SameSite=Null 图所示:

cookie 安全且 SameSite=Null 验证