From 5eb7a2e74b22173c2101dcbda571fc3b861b0014 Mon Sep 17 00:00:00 2001 From: ciel Date: Fri, 9 Aug 2019 16:00:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=8A=9B?= =?UTF-8?q?=E5=87=BA=E7=BB=9F=E4=B8=80=E7=8A=B6=E6=80=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++++---- src/Response/ExceptionReport.php | 5 +++++ src/config.php | 3 +++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2bc08a2..35f6eb4 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ > - [x] jwt-auth用户认证与无感知自动刷新 > - [x] jwt-auth多角色认证不串号 > - [x] 单一设备登陆 - +> - [x] 异常捕获,http状态码统一 ----- ## 安装 @@ -274,8 +274,13 @@ protected $routeMiddleware = [ ``` 2.路由器修改 ``` -Route::middleware('api.refresh')->group(function () { - // jwt认证路由以及无感刷新路由 +Route::middleware('api.refresh:api')->group(function () { + // api看守器登陆授权 + ... +}); + +Route::middleware('api.refresh:admin')->group(function () { + // admin看守器登陆授权 ... }); ``` @@ -292,7 +297,7 @@ class LoginController extends Controller * 自动刷新用户认证 > * 捕获到了 token 过期所抛出的 TokenExpiredException异常 > * 刷新用户的 token `$token = $this->auth->refresh();` -> * 使用一次性登录以保证此次请求的成功 +> * 使用一次性登录以保证此次请求的成功 注意需要设置config jwt宽限时间 > * `Auth::guard('api')->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub']);` > * 在响应头中返回新的 token `$this->setAuthenticationHeader($next($request), $token);` diff --git a/src/Response/ExceptionReport.php b/src/Response/ExceptionReport.php index e9e6edb..b450afc 100644 --- a/src/Response/ExceptionReport.php +++ b/src/Response/ExceptionReport.php @@ -199,6 +199,11 @@ public function report() $message = current($this->exception->validator->errors()->all()); } + // 设置了强制状态码 + if (config('laravel_api.exception.force_http_code')) { + $httpCode = config('laravel_api.exception.force_http_code'); + } + return $this->setHttpCode($httpCode)->setStatusCode($statusCode)->message($message); } } diff --git a/src/config.php b/src/config.php index ef60e53..ebf28be 100644 --- a/src/config.php +++ b/src/config.php @@ -25,6 +25,9 @@ ], ], 'exception' => [ + // 是否强制http状态码。null,空,false,表示不是强制 + 'force_http_code' => 200, + // 在获取不到异常信息的状态码时设置状态码,设置force_http_code时,不生效 'default_http_code' => 500, 'do_report' => [ UnauthorizedHttpException::class => [