Skip to content

Commit 5eb7a2e

Browse files
committed
增加异常抛出统一状态码
1 parent cff8505 commit 5eb7a2e

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
> - [x] jwt-auth用户认证与无感知自动刷新
4545
> - [x] jwt-auth多角色认证不串号
4646
> - [x] 单一设备登陆
47-
47+
> - [x] 异常捕获,http状态码统一
4848
-----
4949

5050
## 安装
@@ -274,8 +274,13 @@ protected $routeMiddleware = [
274274
```
275275
2.路由器修改
276276
```
277-
Route::middleware('api.refresh')->group(function () {
278-
// jwt认证路由以及无感刷新路由
277+
Route::middleware('api.refresh:api')->group(function () {
278+
// api看守器登陆授权
279+
...
280+
});
281+
282+
Route::middleware('api.refresh:admin')->group(function () {
283+
// admin看守器登陆授权
279284
...
280285
});
281286
```
@@ -292,7 +297,7 @@ class LoginController extends Controller
292297
* 自动刷新用户认证
293298
> * 捕获到了 token 过期所抛出的 TokenExpiredException异常
294299
> * 刷新用户的 token `$token = $this->auth->refresh();`
295-
> * 使用一次性登录以保证此次请求的成功
300+
> * 使用一次性登录以保证此次请求的成功 注意需要设置config jwt宽限时间
296301
> * `Auth::guard('api')->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub']);`
297302
> * 在响应头中返回新的 token `$this->setAuthenticationHeader($next($request), $token);`
298303

src/Response/ExceptionReport.php

+5
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ public function report()
199199
$message = current($this->exception->validator->errors()->all());
200200
}
201201

202+
// 设置了强制状态码
203+
if (config('laravel_api.exception.force_http_code')) {
204+
$httpCode = config('laravel_api.exception.force_http_code');
205+
}
206+
202207
return $this->setHttpCode($httpCode)->setStatusCode($statusCode)->message($message);
203208
}
204209
}

src/config.php

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
],
2626
],
2727
'exception' => [
28+
// 是否强制http状态码。null,空,false,表示不是强制
29+
'force_http_code' => 200,
30+
// 在获取不到异常信息的状态码时设置状态码,设置force_http_code时,不生效
2831
'default_http_code' => 500,
2932
'do_report' => [
3033
UnauthorizedHttpException::class => [

0 commit comments

Comments
 (0)