从 iframe 内部发布时 Laravel TokenMismatchExpection

Jun*_*ior 5 php laravel laravel-5 laravel-5.1 laravel-5.2

我有一个正在运行的页面http://some.example.com/myiframes/default.aspx。这个页面有一个 iframe。iframe 源/包含 Laravel 5.2 基础应用程序。

我的 Laravel 页面 URL“这是 iframe 的来源”是https://laravel.example.com

https://laravel.example.com有一个带有提交按钮的表单。当用户点击它时,他/她会点击同一域上的另一条路线,即https://laravel.example.com/disply/survey

但是每次,我提交表单时都会收到以下异常

TokenMismatchException in VerifyCsrfToken.php line 67:
Run Code Online (Sandbox Code Playgroud)

为了确保我清楚,在页面上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>
Run Code Online (Sandbox Code Playgroud)

该表单在 iframe 之外完美运行。只有当我在 iframe 内时才会出现问题。

我打开了VerifyCsrfToken位于的类App\Http\Middleware并添加 http://some.example.com到 $except 数组中,但没有解决问题。

protected $except = [
    'http://some.example.com'
];
Run Code Online (Sandbox Code Playgroud)

什么可能导致这个问题?我该如何解决问题?

编辑,这是我的模板

{!! 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>
Run Code Online (Sandbox Code Playgroud)

Ben*_*son 3

鉴于您在原始问题中提供的详细信息,Laravel 的行为似乎与拒绝您提交表单时应有的行为完全一样。

用户代理正在浏览http://some.example.com并通过 iframePOST将表单访问https://laravel.example.com 。如果我没记错的话,这正是 CSRF 令牌旨在防止的行为。

这个问题甚至可能是Laravel 5 TokenMismatchException 仅在 iFrame 中的重复。我同意那里接受的答案。

如果这是内部站点并且您愿意接受相关风险,则可以为您正在访问的路由添加例外POST,如上述答案中所述。