mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 17:07:49 +08:00
feat : jwt
Some checks are pending
Build Docker / build (push) Waiting to run
Some checks are pending
Build Docker / build (push) Waiting to run
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Exception\Handler;
|
namespace App\Exception\Handler;
|
||||||
|
|
||||||
use App\Constants\ResultCode;
|
use App\Constants\ResultCode;
|
||||||
|
use App\Exception\ErrException;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
@@ -30,17 +31,24 @@ class JwtExceptionHandler extends BaseErrExceptionHandler
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function modifyException(Throwable $e): Throwable
|
protected function modifyException(JWTException $e): Throwable
|
||||||
{
|
{
|
||||||
// 根据不同的异常类型设置不同的code和message
|
// 根据不同的异常类型设置不同的code和message
|
||||||
switch ($e->getMessage()) {
|
switch ($e->getMessage()) {
|
||||||
case 'The token is expired':
|
case 'The token is expired':
|
||||||
$e->code = ResultCode::JWT_EXPIRED;
|
$code = ResultCode::JWT_EXPIRED;
|
||||||
$e->message = 'token已过期';
|
$message = 'token已过期';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$e->code = ResultCode::JWT_ERROR;
|
$code = ResultCode::JWT_ERROR;
|
||||||
$e->message = 'token错误';
|
$message = 'token错误';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method_exists($e, 'setCustomCode')) {
|
||||||
|
$e->setCustomCode($code);
|
||||||
|
}
|
||||||
|
if (method_exists($e, 'setCustomMessage')) {
|
||||||
|
$e->setCustomMessage($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $e;
|
return $e;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||||||
* @contact group@hyperf.io
|
* @contact group@hyperf.io
|
||||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'handler' => [
|
'handler' => [
|
||||||
'http' => [
|
'http' => [
|
||||||
@@ -16,6 +17,7 @@ return [
|
|||||||
App\Exception\Handler\ErrExceptionHandler::class,
|
App\Exception\Handler\ErrExceptionHandler::class,
|
||||||
App\Exception\Handler\ValidationExceptionHandler::class,
|
App\Exception\Handler\ValidationExceptionHandler::class,
|
||||||
App\Exception\Handler\AppExceptionHandler::class,
|
App\Exception\Handler\AppExceptionHandler::class,
|
||||||
|
App\Exception\Handler\JwtExceptionHandler::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user