Laravel 捕获 TokenMismatchException [英] Laravel catch TokenMismatchException

查看:33
本文介绍了Laravel 捕获 TokenMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 try catch 块捕获 TokenMismatchException 吗?我希望它显示实际页面并仅显示错误消息,而不是显示显示VerifyCsrfToken.php 第 46 行中的 TokenMismatchException..."的调试页面.

我对 CSRF 没有任何问题,我只是希望它仍然显示页面而不是调试页面.

复制(使用火狐):步骤:

  1. 打开页面(http://example.com/login)
  2. 清除 Cookie(域、路径、会话).我在这里使用 Web 开发者工具栏插件.
  3. 提交表单.

实际结果:显示糟糕,好像出了点问题"页面.预期结果:仍然显示登录页面,然后传递令牌不匹配"之类的错误.

请注意,当我清除 cookie 时,我没有刷新页面,以便令牌生成新密钥并强制其出错.

更新(添加表格):

 
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"/><div class="form-group"><label for="txtCode" class="col-sm-1 control-label">Code</label><div class="col-sm-11"><input type="text" name="txtCode" id="txtCode" class="form-control" placeholder="Code"/>

<div class="form-group"><label for="txtDesc" class="col-sm-1 control-label">描述</label><div class="col-sm-11"><input type="text" name="txtDesc" id="txtDesc" class="form-control" placeholder="Description"/>

<div class="form-group"><label for="cbxInactive" class="col-sm-1 control-label">Inactive</label><div class="col-sm-11"><div class="checkbox"><标签><input type="checkbox" name="cbxInactive" id="cbxInactive" value="inactive"/>&nbsp;<span class="check"></span>

<div class="form-group"><div class="col-sm-12"><button type="submit" class="btn btn-primary pull-right"><i class="fa fa-save fa-lg"></i>保存

</表单>

这里没什么特别的.只是一个普通的形式.就像我所说的那样,表格工作得很好.只是在我说上述步骤时,由于TOKEN已过期而出错.我的问题是,表格应该那样做吗?我的意思是,当我清除 cookie 和 session 时,我也需要重新加载页面?CSRF 就是这样工作的吗?

解决方案

您可以在AppExceptionsHandler.php

中处理TokenMismatchException异常

Can the TokenMismatchException be catched using try catch block? Instead of displaying the debug page that shows the "TokenMismatchException in VerifyCsrfToken.php line 46...", I want it to display the actual page and just display an error message.

I have no problems with the CSRF, I just want it to still display the page instead of the debug page.

To replicate (using firefox): Steps:

  1. Open page (http://example.com/login)
  2. Clear Cookies (Domain, Path, Session). I am using web developer toolbar plugin here.
  3. Submit form.

Actual Results: "Whoops, looks like something went wrong" page displays. Expected Results: Still display the login page then pass an error of "Token mismatch" or something.

Notice that when I cleared the cookies, I didn't refresh the page in order for the token to generate a new key and force it to error out.

UPDATE (ADDED FORM):

        <form class="form-horizontal" action="<?php echo route($formActionStoreUrl); ?>" method="post">
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>" />
        <div class="form-group">
            <label for="txtCode" class="col-sm-1 control-label">Code</label>
            <div class="col-sm-11">
                <input type="text" name="txtCode" id="txtCode" class="form-control" placeholder="Code" />
            </div>
        </div>
        <div class="form-group">
            <label for="txtDesc" class="col-sm-1 control-label">Description</label>
            <div class="col-sm-11">
                <input type="text" name="txtDesc" id="txtDesc" class="form-control" placeholder="Description" />
            </div>
        </div>
        <div class="form-group">
            <label for="cbxInactive" class="col-sm-1 control-label">Inactive</label>
            <div class="col-sm-11">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="cbxInactive" id="cbxInactive" value="inactive" />&nbsp;
                        <span class="check"></span>
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <button type="submit" class="btn btn-primary pull-right"><i class="fa fa-save fa-lg"></i> Save</button>
            </div>
        </div>
    </form>

Nothing really fancy here. Just an ordinary form. Like what I've said, the form is WORKING perfectly fine. It is just when I stated the above steps, it errors out due to the TOKEN being expired. My question is that, should the form behave that way? I mean, when ever I clear cookies and session I need to reload the page too? Is that how CSRF works here?

解决方案

You can handle TokenMismatchException Exception in AppExceptionsHandler.php

<?php namespace AppExceptions;
use Exception;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use IlluminateSessionTokenMismatchException;


class Handler extends ExceptionHandler {


    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        'SymfonyComponentHttpKernelExceptionHttpException'
    ];
    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }
    /**
     * Render an exception into an HTTP response.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Exception  $e
     * @return IlluminateHttpResponse
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof TokenMismatchException){
            // Redirect to a form. Here is an example of how I handle mine
            return redirect($request->fullUrl())->with('csrf_error',"Oops! Seems you couldn't submit form for a long time. Please try again.");
        }

        return parent::render($request, $e);
    }
}

这篇关于Laravel 捕获 TokenMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆