解决:打开文件:app\Http\Middleware\VerifyCsrfToken.php,
- 全部关闭
use Closure;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
//
];
/**
* 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;
}
}
2. 部分关闭
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'v1/*'
];
}
这样,post请求http://test.com/v1/***的接口都可以不用csrf验证
在Laravel框架中遇到Post请求返回419(CSRF token mismatch)或500错误,可以修改appHttpMiddlewareVerifyCsrfToken.php中间件来解决。可以选择全部关闭CSRF验证,或者部分关闭,例如将'v1/*'排除在外,使得所有以/v1/开头的接口不再进行CSRF验证。
662





