Laravel 5.5 使用 api.token 疑问
升级到5.5后,在日志里发现了大量的报错信息,如下:
[2017-09-12 11:08:20] local.ERROR: The resource owner or authorization server denied the request. {"exception":"[object] (League\\OAuth2\\Server\\Exception\\OAuthServerException(code: 9): The resource owner or authorization
..... 省略
>handle(Object(Illuminate\\Http\\Request))
#41 {main}
但是api访问均正常返回。于是开始分析。
\vendor\league\oauth2-server\src\AuthorizationValidators\BearerTokenValidator.php(93):
League\OAuth2\Server\Exception\OAuthServerException::accessDenied('The JWT string ...');
throw OAuthServerException::accessDenied($exception->getMessage());
public static function accessDenied($hint = null, $redirectUri = null)
{
return new static(
'The resource owner or authorization server denied the request.',
9,
'access_denied',
401,
$hint,
$redirectUri
);
}
很奇怪的是,既然抛出了这个异常,那为什么访问是正常的呢???
我在请求api时候
axios.defaults.headers.common['X-CSRF-TOKEN'] = window._csrf
axios.defaults.headers.common['Authorization'] = `Bearer ${window._token}`
今天跟踪代码,发现了这个问题所在了。应该是你调用了Auth::user()方法,触发了一个token检测流程。

这行代码就是检测token,方法里面有异常抛出,
这儿捕获到这个异常后,就向框架报告了这个事件。
这儿就是调用logger对象,把这个事件写入日志系统。但是这样不影响api请求的,user方法会返回为空null。
@Aaron 请问下
League\OAuth2\Server\Exception\OAuthServerException
这个异常我可以在App\Exceptions\Handler
里面的report
函数里捕获, 但是在render
里面捕获不了。 因为现在这个异常返回的是状态码401
然后内容为Unauthorized.
。因为我想把这个异常按我自己定义的格式输出。@tiandaye 已解决,在中间件中。
@tiandaye 我今天也遇到了类似的情况,是
App\\Http\\Middleware\\Authenticate
这个中间件调用App\\Http\\Middleware\\Authenticate
抛出的异常,但是我在中间件里面捕获这个异常捕获不到@tiandaye 请问怎么解决的
@zrt
我是捕获不到这个异常,因为在这一步就被response
在
App\Exceptions\Handler.php
文件中增加以下错误报告忽略项即可解决