我已经安装了Laravel Passport并根据文档进行了配置。从我的VueJS文件中调用axios.get
时,第一个调用按预期工作。 laravel_session
请求Cookie被注入到请求中,身份验证通过,返回资源。
当我尝试再次调用axios.get
方法时出现问题。我的用例是搜索功能。每当用户使用以下代码键入文本字段时,我都会调用/api/banking/accounts/search/{search-term}
:
remoteMethod(query) {
if (query !== '') {
this.loading = true;
axios.get(
`/api/banking/accounts/search/${escape(query)}`
).then(res => {
this.destinationAccountDirectory = res.data;
this.loading = false;
});
} else {
this.destinationAccountDirectory = [];
}
},
此代码在路由中没有任何auth:api
中间件的情况下工作正常,并且第一次使用auth:api
中间件。从下面的屏幕截图中可以看出,laravel_token
值会更改,并在后续API调用时被拒绝。
**我试图删除在护照安装期间添加到\Laravel\Passport\Http\Middleware\CreateFreshApiToken
中间件组的web
,这似乎暂时解决了问题,直到我收到419请求不久之后。什么可能导致新的laravel_tokens被拒绝? **
答案 0 :(得分:0)
我通过从API路由中删除V(mymodule)
中间件来解决这个问题。为什么它首先出现在那里,我不知道。
我从
改变了我的api.phpweb
到
Route::group([
'middleware' => [
'web',
'auth:api']], function() {
Route::post('/banking/transactions', 'TransactionController@store');
Route::get('/banking/accounts', 'BankAccountDirectoryController@index');
Route::get('/accounts/{account}', 'BankAccountDirectoryController@show');
Route::get('/banking/accounts/search/{term?}', 'BankAccountDirectoryController@search');
});
API路由是否应位于Web组下以从中间件中受益,还是纯粹用于UI?我可以安全地做到这一点吗?