【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
laravel6默认的token失效或验证错误,返回内容代码在
vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php
protected function unauthenticated($request, array $guards)
{
throw new AuthenticationException(
'Unauthenticated.', $guards, $this->redirectTo($request)
);
}
默认或返回401, 显然这样对接口不友好, 自定义返回内容,只需重写该方法覆盖即可
在 app\Http\Middleware\Authenticate.php 添加如下方法
protected function unauthenticated($request, array $guards)
{
if (in_array('api', $guards)){ //这里可指定guard, 也可去掉
die(json_encode(['status' => 401, 'msg' => 'token已失效或验证错误,请重新登陆']));
}
throw new AuthenticationException(
'Unauthenticated.', $guards, $this->redirectTo($request)
);
}
来源:oschina
链接:https://my.oschina.net/u/3403514/blog/3147228