Laravel 从 iframe 中发帖时出现 TokenMismatchException
Laravel TokenMismatchExpection when posting from inside an iframe
我在 http://some.example.com/myiframes/default.aspx
上有一个页面 运行。此页面有一个 iframe。 iframe source/contains 一个 Laravel 5.2 基础应用程序。
我的 Laravel 页面 URL "which is the source of the iframe" 是 https://laravel.example.com。
https://laravel.example.com 有一个带有提交按钮的表单。当用户点击它时,he/she 会点击同一域上的另一条路由,即 https://laravel.example.com/disply/survey
但每次我提交表单时都会收到以下异常
TokenMismatchException in VerifyCsrfToken.php line 67:
为了确保我清楚,在页面上 http://some.example.com/myiframes/default.aspx
我的代码看起来像这样
在我的 laravel 应用程序上 https://laravel.example.com
这是我的表格
<form method="POST" action="https://laravel.example.com/disply/survey" accept-charset="UTF-8" class="form">
<input name="_token" type="hidden" value="Dk6SN4WzO4brbvdnBO6JZ7e1lBGjmYz8GQJ1lYFo">
<input name="survey_id" type="hidden" value="10">
<input name="call_id" type="hidden" value="667">
<input name="pools" type="hidden">
<input name="alt_id_1" type="hidden" value="250">
<input name="alt_id_2" type="hidden" value="5">
<input name="alt_id_3" type="hidden">
<input name="alt_id_4" type="hidden">
<input name="alt_id_5" type="hidden">
<input name="alt_id_6" type="hidden">
<input name="alt_id_7" type="hidden">
<input name="alt_id_8" type="hidden">
<input name="alt_id_9" type="hidden">
<input name="alt_id_10" type="hidden">
<input name="alt_string_1" type="hidden">
<input name="alt_string_2" type="hidden">
<input name="alt_string_3" type="hidden">
<input name="alt_string_4" type="hidden">
<input name="alt_string_5" type="hidden">
<input name="alt_string_6" type="hidden">
<input name="alt_string_7" type="hidden">
<input name="alt_string_8" type="hidden">
<input name="alt_string_9" type="hidden">
<input name="alt_string_10" type="hidden">
<div class="text-center">
<input class="btn btn-primary" type="submit" value="Start Survey">
</div>
</form>
表单在 iframe 之外工作得很好。只有当我在 iframe 中时才会出现此问题。
我打开位于App\Http\Middleware
的VerifyCsrfToken
class并添加
http://some.example.com
进入 $except 数组,但未能解决问题。
protected $except = [
'http://some.example.com'
];
是什么导致了这个问题?
我该如何解决这个问题?
已编辑,这是我的模板
{!! Form::open([
'url' => route('my.surveys.display'),
'class' => 'form',
'method' => 'post'
]) !!}
{!! Form::hidden('survey_id', $survey_id) !!}
{!! Form::hidden('call_id', $call_id) !!}
{!! Form::hidden('pools', $pools) !!}
{!! Form::hidden('call_type', $type) !!}
{!! Form::hidden('alt_id_1', $alt_id_1) !!}
{!! Form::hidden('alt_id_2', $alt_id_2) !!}
{!! Form::hidden('alt_id_3', $alt_id_3) !!}
{!! Form::hidden('alt_id_4', $alt_id_4) !!}
{!! Form::hidden('alt_id_5', $alt_id_5) !!}
{!! Form::hidden('alt_id_6', $alt_id_6) !!}
{!! Form::hidden('alt_id_7', $alt_id_7) !!}
{!! Form::hidden('alt_id_8', $alt_id_8) !!}
{!! Form::hidden('alt_id_9', $alt_id_9) !!}
{!! Form::hidden('alt_id_10', $alt_id_10) !!}
{!! Form::hidden('alt_string_1', $alt_string_1) !!}
{!! Form::hidden('alt_string_2', $alt_string_2) !!}
{!! Form::hidden('alt_string_3', $alt_string_3) !!}
{!! Form::hidden('alt_string_4', $alt_string_4) !!}
{!! Form::hidden('alt_string_5', $alt_string_5) !!}
{!! Form::hidden('alt_string_6', $alt_string_6) !!}
{!! Form::hidden('alt_string_7', $alt_string_7) !!}
{!! Form::hidden('alt_string_8', $alt_string_8) !!}
{!! Form::hidden('alt_string_9', $alt_string_9) !!}
{!! Form::hidden('alt_string_10', $alt_string_10) !!}
<div class="text-center">
{!! Form::submit('Start Survey', ['class' => 'btn btn-primary', 'id' => 'start_survey']) !!}
</div>
按照此步骤操作
https://laravel.com/docs/master/routing#csrf-x-csrf-token
X-CSRF-TOKEN
除了检查 CSRF 令牌作为 POST 参数外,Laravel VerifyCsrfToken 中间件还将检查 X-CSRF-TOKEN 请求 header。例如,您可以将令牌存储在 "meta" 标签中:
创建元标记后,您可以指示像 jQuery 这样的库将令牌添加到所有请求 header 中。这为基于 AJAX 的应用程序提供了简单、方便的 CSRF 保护:
$.ajaxSetup({
header秒:{
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
X-XSRF-TOKEN
Laravel 还将 CSRF 令牌存储在 XSRF-TOKEN cookie 中。您可以使用 cookie 值来设置 X-XSRF-TOKEN 请求 header。一些 JavaScript 框架,例如 Angular,会自动为您执行此操作。您不太可能需要手动使用此值。
鉴于您在原始问题中提供的详细信息,Laravel 似乎在拒绝您提交表单时表现得完全正常。
user-agent 正在通过 iframe 浏览到 http://some.example.com and POST
ing the form to https://laravel.example.com。如果我没记错的话,这正是 CSRF 令牌旨在防止的行为。
这个问题甚至可能与 重复。我同意那里接受的答案。
如果这是一个内部站点并且您愿意接受相关风险,您可以为您POST
访问的路由添加一个例外,如 above-cited 中所述回答。
我在 http://some.example.com/myiframes/default.aspx
上有一个页面 运行。此页面有一个 iframe。 iframe source/contains 一个 Laravel 5.2 基础应用程序。
我的 Laravel 页面 URL "which is the source of the iframe" 是 https://laravel.example.com。
https://laravel.example.com 有一个带有提交按钮的表单。当用户点击它时,he/she 会点击同一域上的另一条路由,即 https://laravel.example.com/disply/survey
但每次我提交表单时都会收到以下异常
TokenMismatchException in VerifyCsrfToken.php line 67:
为了确保我清楚,在页面上 http://some.example.com/myiframes/default.aspx
我的代码看起来像这样
在我的 laravel 应用程序上 https://laravel.example.com
这是我的表格
<form method="POST" action="https://laravel.example.com/disply/survey" accept-charset="UTF-8" class="form">
<input name="_token" type="hidden" value="Dk6SN4WzO4brbvdnBO6JZ7e1lBGjmYz8GQJ1lYFo">
<input name="survey_id" type="hidden" value="10">
<input name="call_id" type="hidden" value="667">
<input name="pools" type="hidden">
<input name="alt_id_1" type="hidden" value="250">
<input name="alt_id_2" type="hidden" value="5">
<input name="alt_id_3" type="hidden">
<input name="alt_id_4" type="hidden">
<input name="alt_id_5" type="hidden">
<input name="alt_id_6" type="hidden">
<input name="alt_id_7" type="hidden">
<input name="alt_id_8" type="hidden">
<input name="alt_id_9" type="hidden">
<input name="alt_id_10" type="hidden">
<input name="alt_string_1" type="hidden">
<input name="alt_string_2" type="hidden">
<input name="alt_string_3" type="hidden">
<input name="alt_string_4" type="hidden">
<input name="alt_string_5" type="hidden">
<input name="alt_string_6" type="hidden">
<input name="alt_string_7" type="hidden">
<input name="alt_string_8" type="hidden">
<input name="alt_string_9" type="hidden">
<input name="alt_string_10" type="hidden">
<div class="text-center">
<input class="btn btn-primary" type="submit" value="Start Survey">
</div>
</form>
表单在 iframe 之外工作得很好。只有当我在 iframe 中时才会出现此问题。
我打开位于App\Http\Middleware
的VerifyCsrfToken
class并添加
http://some.example.com
进入 $except 数组,但未能解决问题。
protected $except = [
'http://some.example.com'
];
是什么导致了这个问题? 我该如何解决这个问题?
已编辑,这是我的模板
{!! Form::open([
'url' => route('my.surveys.display'),
'class' => 'form',
'method' => 'post'
]) !!}
{!! Form::hidden('survey_id', $survey_id) !!}
{!! Form::hidden('call_id', $call_id) !!}
{!! Form::hidden('pools', $pools) !!}
{!! Form::hidden('call_type', $type) !!}
{!! Form::hidden('alt_id_1', $alt_id_1) !!}
{!! Form::hidden('alt_id_2', $alt_id_2) !!}
{!! Form::hidden('alt_id_3', $alt_id_3) !!}
{!! Form::hidden('alt_id_4', $alt_id_4) !!}
{!! Form::hidden('alt_id_5', $alt_id_5) !!}
{!! Form::hidden('alt_id_6', $alt_id_6) !!}
{!! Form::hidden('alt_id_7', $alt_id_7) !!}
{!! Form::hidden('alt_id_8', $alt_id_8) !!}
{!! Form::hidden('alt_id_9', $alt_id_9) !!}
{!! Form::hidden('alt_id_10', $alt_id_10) !!}
{!! Form::hidden('alt_string_1', $alt_string_1) !!}
{!! Form::hidden('alt_string_2', $alt_string_2) !!}
{!! Form::hidden('alt_string_3', $alt_string_3) !!}
{!! Form::hidden('alt_string_4', $alt_string_4) !!}
{!! Form::hidden('alt_string_5', $alt_string_5) !!}
{!! Form::hidden('alt_string_6', $alt_string_6) !!}
{!! Form::hidden('alt_string_7', $alt_string_7) !!}
{!! Form::hidden('alt_string_8', $alt_string_8) !!}
{!! Form::hidden('alt_string_9', $alt_string_9) !!}
{!! Form::hidden('alt_string_10', $alt_string_10) !!}
<div class="text-center">
{!! Form::submit('Start Survey', ['class' => 'btn btn-primary', 'id' => 'start_survey']) !!}
</div>
按照此步骤操作
https://laravel.com/docs/master/routing#csrf-x-csrf-token
X-CSRF-TOKEN
除了检查 CSRF 令牌作为 POST 参数外,Laravel VerifyCsrfToken 中间件还将检查 X-CSRF-TOKEN 请求 header。例如,您可以将令牌存储在 "meta" 标签中:
创建元标记后,您可以指示像 jQuery 这样的库将令牌添加到所有请求 header 中。这为基于 AJAX 的应用程序提供了简单、方便的 CSRF 保护:
$.ajaxSetup({ header秒:{ 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
X-XSRF-TOKEN
Laravel 还将 CSRF 令牌存储在 XSRF-TOKEN cookie 中。您可以使用 cookie 值来设置 X-XSRF-TOKEN 请求 header。一些 JavaScript 框架,例如 Angular,会自动为您执行此操作。您不太可能需要手动使用此值。
鉴于您在原始问题中提供的详细信息,Laravel 似乎在拒绝您提交表单时表现得完全正常。
user-agent 正在通过 iframe 浏览到 http://some.example.com and POST
ing the form to https://laravel.example.com。如果我没记错的话,这正是 CSRF 令牌旨在防止的行为。
这个问题甚至可能与
如果这是一个内部站点并且您愿意接受相关风险,您可以为您POST
访问的路由添加一个例外,如 above-cited 中所述回答。