讨论数量:
在ExceptionHandle中改写处理逻辑或者在App\Http\MiddlewareAuthenticate
下重写unauthenticated
方法抛出自定义错误并自定义处理逻辑。
原因:在继承的中间件中有unauthenticated
方法如下:
/**
* Handle an unauthenticated user.
*
* @param \Illuminate\Http\Request $request
* @param array $guards
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function unauthenticated($request, array $guards)
{
throw new AuthenticationException(
'Unauthenticated.',
$guards,
$this->redirectTo($request)
);
}
若是 api 请求请加上下面请求头,中间件除了json请求才会跳
Content-Type:application/json
X-Requested-With:XMLHttpRequest
若是web请求
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
// return route('login');
return response()->json([
'foo'=>'bar'
]);
}
}
推荐文章: