当前位置:网站首页>laravel postman 提交表单出现419错误。2020年7月6日记。
laravel postman 提交表单出现419错误。2020年7月6日记。
2022-07-01 09:08:00 【你的长情呢~】
最近在学习laraveel入门,学到了上传和下载这个部分,因为还没有学到控制器和视图,所以就用postman模拟提交表单,上传了一个小文件。
但是用postman提交表单上传文件的时候,出现 了419错误,这个是因为csrf的限制。解决方案百度之后的结果大概有3个
第一个做法是注释中间件:
app\Http\Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//注释掉下面这一行
// \App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
第二个做法就是 设置白名单
app\Http\Middleware\VerifyCsrfToken.php
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/** * The URIs that should be excluded from CSRF verification. * * @var array */
protected $except = [
//在这里设置 *
'*'
];
}
这2个步骤的确可以实现功能,但是这2个做法都比较极端,于是尝试了针对某个控制器的某个方法设置白名单,写法如下
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/** * The URIs that should be excluded from CSRF verification. * * @var array */
protected $except = [
//在这里设置 *
// '*'
'[email protected]',
'index/index',
'www.blog.com/scc'
];
}
尝试了三个写法 都不对,用postman上传文件还是419错误,后来研究到了真正的解决方法。
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/** * The URIs that should be excluded from CSRF verification. * * @var array */
protected $except = [
#这个才是根正苗红的写法。
'scc'
];
}
scc是路由标识。
如下
#路由规则
Route::any('scc', '[email protected]');
#控制器的方法
function scc(Request $request){
// echo csrf_token();
$name = $request->file('filename');
echo '<pre>';
print_r($name->path());
echo '</pre>';
echo '<hr>';
die;
}
、
边栏推荐
猜你喜欢

Design and manufacture of simple digital display electronic scale

How to manage fixed assets efficiently in one stop?

队列的实现和应用

Installing Oracle EE

NoSQL数据库的安装和使用

2.4 activation function

如何一站式高效管理固定资产?

【检测技术课案】简易数显电子秤的设计与制作

Bird recognition app

Structure de l'arbre - - - arbre binaire 2 traversée non récursive
随机推荐
Shell script -select in loop
Jetson nano installs tensorflow GPU and problem solving
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
NoSQL数据库的安装和使用
Design and manufacture of simple digital display electronic scale
Software Engineer Interview Question brushing website and experience method
Shell script - positional parameters (command line parameters)
TV size and viewing distance
【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
2.2 【pytorch】torchvision.transforms
JCL and slf4j
Daily office consumables management solution
Daily practice of C language - day 80: currency change
Key points of NFT supervision and overseas policies
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
Jetson Nano 安装TensorFlow GPU及问题解决
Shell脚本-for循环和for int循环
易点易动助力企业设备高效管理,提升设备利用率