laravel post TokenMismatchException in VerifyCsrfToken.php line 53 问题解决方法
2017-11-09 19:14
656 查看
CSRF攻击和漏洞的参考文章:
http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
Laravel默认是开启了CSRF功能,需要关闭此功能有两种方法:
原文地址:点击打开链接
打开文件:app\Http\Kernel.php
把这行注释掉:
打开文件:app\Http\Middleware\VerifyCsrfToken.php
修改为:
CSRF的使用有两种,一种是在HTML的代码中加入:
另一种是使用cookie方式。
使用cookie方式,需要把app\Http\Middleware\VerifyCsrfToken.php修改为:
使用cookie方式的CSRF,可以不用在每个页面都加入这个input的hidden标签。
当然,也可以对指定的表单提交方式使用CSRF,如:
只对GET的方式提交使用CSRF,对POST方式提交表单禁用CSRF
通常使用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
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报错:TokenMismatchException in VerifyCsrfToken.php
- Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法
- Laravel学习之VerifyCsrfToken 报错解决方法详解
- 解决Laravel 5上传大文件时出现TokenMismatchException的问题
- laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
- PHP Warning: Module 'modulename' already loaded in Unknown on line 0 的解决方法
- hadoop问题Type mismatch in value from map解决方法
- PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
- [php错误]PHP中Notice: unserialize(): Error at offset of bytes in on line 的解决方法
- java.lang.Exception: libsvm classes not in CLASSPATH!问题解决方法
- PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
- $_POST传值定义的变量在直接加载这个php文件的时候会报错的问题解决方法
- php 分页显示数据 post参数传递问题解决方法
- PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等)
- php post大量数据时发现数据丢失问题解决方法
- PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等)
- php post大量数据时发现数据丢失问题解决方法
- PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法