Laravel 5.1防止CSRF不匹配引发异常 [英] Laravel 5.1 prevent CSRF mismatch from throwing exception

查看:90
本文介绍了Laravel 5.1防止CSRF不匹配引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了向用户抛出CSRF异常的问题.发生这种情况的原因完全是无辜的,例如,如果某人在最终提交表单时花了太长时间来填写表单,则会话已过期且令牌不匹配.现在显然这是一个错误,但不需要杀死所有内容并引发异常.

I am getting issues with CSRF exceptions being thrown to the user. They happen for perfectly innocent reasons like if someone takes too long to fill out a form when they finally submit it the session has expired and the tokens don't match. Now obviously this is an error but it doesn't need to kill everything and throw an exception.

有没有一种方法可以让它设置一条Flash消息,然后重定向回原始页面.我不想禁用CSRF保护,我只是想更优雅地处理错误.

Is there a way to just get it to set a flash message instead and redirect back to the original page. I don't want to disable CSRF protection I just want the errors to be handled a bit more gracefully.

推荐答案

可以在app/Handler.php

This can be handled in app/Handler.php

public function render($request, Exception $e)
{
    return parent::render($request, $e);
}

对此:

public function render($request, Exception $e)
{
    if ($e instanceof \Illuminate\Session\TokenMismatchException){

        return redirect($request->fullUrl())->with('error',"Sorry your session has expired please resubmit your request.");
    }
    return parent::render($request, $e);
}

这篇关于Laravel 5.1防止CSRF不匹配引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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