我有一个在URL末尾有一个动态参数的路由。在此路径中,我使用post方法获取从外部API发送的数据。由于外部接口发送post请求时出现419 page expired
错误,需要对该路由关闭csrf防护。
相关路由:
Route::group(['middleware' => ['auth:student']], function (){
Route::post('Result', 'ExamController@Result')->name('exam.Result');
}
我的URL示例:
http://localhost.dev/student/Result?Id=N7utfGkwOLebxMWGA5iUC4S23jgRzW
我尝试将此代码添加到App\Http\Middleware
的VerifyCsrfToken
文件中
protected $except = [
'student/Result/*',
];
它不起作用。但当我尝试student/*
时,它工作得很完美。然而,禁用所有student
路径的csrf保护并不是我想要的。
我也尝试了这种方法,通过在this thread上获取参考
Route::post('Result', [
'uses' => 'ExamController@Result',
'nocsrf' => 'true'
])->name('exam.Result');
这也不管用。
如何在此场景下关闭csrf防护?
发布于 2019-08-17 19:26:46
你在App\Http\Middleware
犯了一个打字错误,而不是:
protected $except = [
'student/Result/*',
];
您需要使用:
protected $except = [
'student/Result',
];
此外,根据documentation,您可以指定需要排除的完整url:
protected $except = [
'http://localhost.dev/student/Result',
];
请注意,您不需要在此处添加路由的参数部分( ?
sign之后的所有内容,例如?Id=N7utfGkwOLebxMWGA5iUC4S23jgRzW
)。
发布于 2019-08-17 19:14:29
试试这个(去掉斜杠和星号):
protected $except = [
'student/Result',
];
https://stackoverflow.com/questions/57535700
复制相似问题