laravel post TokenMismatchException in VerifyCsrfToken.php line 53 问题解决方法
来源:互联网 发布:软件系统售后服务方案 编辑:程序博客网 时间:2024/05/16 17:34
CSRF攻击和漏洞的参考文章:
http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
Laravel默认是开启了CSRF功能,需要关闭此功能有两种方法:
原文地址:点击打开链接
方法一
打开文件:app\Http\Kernel.php
把这行注释掉:
'App\Http\Middleware\VerifyCsrfToken'
方法二
打开文件:app\Http\Middleware\VerifyCsrfToken.php
修改为:
<?php namespace App\Http\Middleware;use Closure;use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;class VerifyCsrfToken extends BaseVerifier { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // 使用CSRF //return parent::handle($request, $next); // 禁用CSRF return $next($request); }}
CSRF的使用有两种,一种是在HTML的代码中加入:
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
另一种是使用cookie方式。
使用cookie方式,需要把app\Http\Middleware\VerifyCsrfToken.php修改为:
<?php namespace App\Http\Middleware;use Closure;use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;class VerifyCsrfToken extends BaseVerifier { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { return parent::addCookieToResponse($request, $next($request)); }}
使用cookie方式的CSRF,可以不用在每个页面都加入这个input的hidden标签。
当然,也可以对指定的表单提交方式使用CSRF,如:
<?php namespace App\Http\Middleware;use Closure;use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;class VerifyCsrfToken extends BaseVerifier { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // Add this: if($request->method() == 'POST') { return $next($request); } if ($request->method() == 'GET' || $this->tokensMatch($request)) { return $next($request); } throw new TokenMismatchException; }}
只对GET的方式提交使用CSRF,对POST方式提交表单禁用CSRF
修改CSRF的cookie名称方法
通常使用CSRF时,会往浏览器写一个cookie,如:
要修改这个名称值,可以到打开这个文件:vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php
找到”XSRF-TOKEN“,修改它即可。
当然,你也可以在app\Http\Middleware\VerifyCsrfToken.php文件中,重写addCookieToResponse(...)方法做到。
另外,如需要对指定的页面不使用CSRF,可以参考如下文章:
http://www.camroncade.com/disable-csrf-for-specific-routes-laravel-5/
转载来自:http://blog.csdn.net/app_ios/article/details/67634627
- laravel post TokenMismatchException in VerifyCsrfToken.php line 53 问题解决方法
- TokenMismatchException in VerifyCsrfToken.php line
- laravel ajax POST请求 错误 TokenMismatchException in VerifyCsrfToken.php (line 68)
- laravel报错:TokenMismatchException in VerifyCsrfToken.php
- Laravel NotFoundHttpException in RoutesRequests.php line 442
- laravel报TokenMismatchException
- Laravel : InvalidArgumentException in Carbon.php line 425: Data missing
- Laravel VerifyCsrfToken 报错解决
- laravel中TokenMismatchException异常处理
- Laravel 的 TokenMismatchException 报错
- laravel post方法请求失败
- php 分页显示数据 post参数传递问题解决方法
- php 分页显示数据 post参数传递问题解决方法
- Laravel学习之VerifyCsrfToken 报错解决方法详解
- Notice: Undefined offset: 4 in d:\...\chaxun\rbora.php on line 96错误方法
- php模拟post方法
- php curl post 方法
- unexpected T_ELSE in if_expression.php on line
- Sulley实例:测试easyftp
- 2236 Wireless Network 并查集
- JS
- 学完软件工程之后的总结
- hyper-v虚拟机内为centos6的虚拟机动态扩容硬盘
- laravel post TokenMismatchException in VerifyCsrfToken.php line 53 问题解决方法
- kmp板子 洛谷P3375
- java 字符串截取的三种方法(推荐)
- HDU-1532 Drainage Ditches(最大流基础)
- U
- 图片上传各参数含义
- python+uiautomator: IOError: RPC server not started!
- 习题5 5.1
- Java反射机制