【发布时间】:2017-06-21 04:58:36
【问题描述】:
我已按照此处的指南进行操作:https://laravel.com/docs/5.4/passport#consuming-your-api-with-javascript
使用 axios:
...
mounted: function() {
axios.get('/api/user')
.then(function (response) {
console.log(response)
})
.catch(function (response) {
console.error(response);
});
},
但响应总是未经身份验证,我检查是否存在 laravel_token cookie,它是:
我在 apache2 ( docker ) 上运行
---- 更新 --
在调试时,它实际上是在TokenGuard 中此方法中失败的 xsrf 令牌:
/**
* Authenticate the incoming request via the token cookie.
*
* @param Request $request
* @return mixed
*/
protected function authenticateViaCookie($request)
{
try {
$token = $this->decodeJwtTokenCookie($request);
} catch (Exception $e) {
return;
}
# This is not passing:
if (! $this->validCsrf($token, $request) ||
time() >= $token['expiry']) {
return;
}
if ($user = $this->provider->retrieveById($token['sub'])) {
return $user->withAccessToken(new TransientToken);
}
}
我在 boostrap.js 中有适当的设置:
window.axios = require('axios');
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest'
};
【问题讨论】:
-
我也有类似的问题。看看你能不能找到答案stackoverflow.com/questions/39228194/…
-
@RikardOlsson 已更新
标签: laravel jwt laravel-5.4 laravel-passport