【问题标题】:Why do I get a 419 page when I have my token added?为什么添加令牌后会出现 419 页面?
【发布时间】:2021-09-04 22:41:46
【问题描述】:

我想在自己的视图中编辑一篇博客文章,但是当我将编辑内容进行更改时,我得到一个 419 页面。

这是我的编辑视图,用于编辑由其 id 指定的相关博客:

<div id="body" style="color:#333">
    <h1 style="color:#333">Update blog</h1>

    <form method="POST" action="{{route('blogSingle',$blog->id)}}">
        @method('put')
        @csrf
        <div class="field">
            <label for="title" class="label" style='font-size:1.2rem'>Title</label>

            <div class="control">
                <input type="text" class="input" name="title" id="title" placeholder="Blog Title" value="{{$blog->title}}">
            </div>

        </div>
        <div class="field">
            <label for="body" class="label" style='font-size:1.2rem'>Body</label>

            <div class="control">
                <textarea type="text" class="textarea" name="body" id="body" placeholder="Blog Description">{{$blog->body}}</textarea>
            </div>

        </div>

        <div class="field is-grouped">
            <div class="control">
                <button type="submit" class="btn btn-secondary">Submit</button>
            </div>
        </div>
    </form>
</div>

我还为每个博客创建了一个视图,在其中,我单击一个按钮以重定向到博客编辑视图,如下所示:

<div id="body">
    {{-- Add a variable blog post here --}}
    <h1><span>blog single post</span> <span><a href="{{route('blog-edit',$blog->id)}}" class="btn btn-secondary">Edit Blog</a></span></h1>
    <div>
        <img src="images/grew-a-mustache.jpg" alt="Mustache">
        <div class="article">
            <h2 class="lead">{{$blog->title}}</h2>
            <p class="lead">
                {{$blog->body}}
            </p>
        </div>
    </div>
</div>

我所涉及的路线如下:

Route::get('blog-single/{blog}',[BlogController::class,'show'])->name('blogSingle');
Route::get('blog-single/{blog}/edit', [BlogController::class, 'edit'])->name('blog-edit');
Route::put('blog-single/{blog}',[BlogController::class, 'update']);

为什么我的 419 页面仍然过期?我什至在发送之前检查了页面,我在这里清楚地看到了我的令牌:

【问题讨论】:

  • 你在那个页面上等了很久吗? 419 表示授权已过期。
  • 我几乎立即进行了编辑,只是为了测试它是否有效。但是,我昨天从专门用于创建新博客的 create-blog 视图添加了这些文章,并且它首先插入了 @csrf。我不确定它是否重要。
  • 您可以尝试用@method('PUT') 替换@method('put') 吗?
  • 我刚刚做了,但我仍然得到同样的东西。我昨天已将 SESSION_DOMAIN 添加到我的 env 文件中,域为 127.0.01:8000。我添加它是因为当我尝试创建一个新博客时,我的 419 页面也已过期,那是我第一次在我的创建博客视图中添加 @csrf。

标签: php laravel csrf


【解决方案1】:

仅当 VerifyCsrfToken 中间件无法验证您的令牌时才会抛出 419。拥有@csrf 并不能保证您的令牌不会失败。您的令牌可能失败的原因:

  • 您的会话具有不同的令牌;检查session()-&gt;token() 的结果,看看它是否与@csrf 字段中的令牌匹配。如果没有,请尝试 session()-&gt;flush() 并在必要时重新进行身份验证。
  • 在覆盖 CSRF 的 @csrf 之后,您有另一个以 _token 作为名称的表单输入
  • 您有中间件操作令牌
  • 您弄乱了供应商文件,因此tokensMatch 功能无法正常运行(如果您认为您已经这样做了,您可以删除您的供应商文件夹,然后运行composer clearcachecomposer install )

我想补充一下,你没有路由到你的更新方法:

&lt;form method="POST" action="{{route('blogSingle',$blog-&gt;id)}}"&gt;

正在调用 show 方法:

Route::get('blog-single/{blog}', [BlogController::class,'show'])-&gt;name('blogSingle');

确实 工作,因为您指定的方法和 URL 是相同的,但这不是一个好习惯。如果您明确调用更新路由(您甚至还没有给出名称)会更好。使用占位符名称而不是让路由按顺序填充占位符也是更好的做法:

Route::put('blog-single/{blog}',[BlogController::class, 'update'])-&gt;name('blog-update');

&lt;form method="POST" action="{{route('blog-update', ['blog' =&gt; $blog])}}"&gt;

您也可以使用compact将博客快速绑定到占位符。

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2021-10-10
    • 2022-06-13
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多