diff --git a/README.md b/README.md index cec9b44..df6d64c 100644 --- a/README.md +++ b/README.md @@ -242,4 +242,4 @@ class IndexController extends AbstractController 例如:`$this->jwt->getParserData()` ##### 12、建议 -> 目前 `jwt` 抛出的异常目前有两种类型 `Phper666\JwtAuth\Exception\TokenValidException` 和 `Phper666\JwtAuth\Exception\JWTException,TokenValidException` 异常为 token 验证失败的异常,会抛出 `401` ,`JWTException` 异常会抛出 `500`,最好你们自己在项目异常重新返回错误信息 +> 目前 `jwt` 抛出的异常目前有两种类型 `Phper666\JwtAuth\Exception\TokenValidException` 和 `Phper666\JwtAuth\Exception\JWTException,TokenValidException` 异常为 token 验证失败的异常,会抛出 `401` ,`JWTException` 异常会抛出 `400`,最好你们自己在项目异常重新返回错误信息 diff --git a/src/Jwt.php b/src/Jwt.php index 166aca2..2259ae8 100644 --- a/src/Jwt.php +++ b/src/Jwt.php @@ -39,7 +39,7 @@ public function getToken(array $claims, $isInsertSsoBlack = true) $uniqid = uniqid(); } else { // 单点登录 if (empty($claims[$this->ssoKey])) { - throw new JWTException("There is no {$this->ssoKey} key in the claims", 500); + throw new JWTException("There is no {$this->ssoKey} key in the claims", 400); } $uniqid = $claims[$this->ssoKey]; } @@ -74,7 +74,7 @@ public function getToken(array $claims, $isInsertSsoBlack = true) public function refreshToken() { if (!$this->getHeaderToken()) { - throw new JWTException('A token is required', 500); + throw new JWTException('A token is required', 400); } $claims = $this->blacklist->add($this->getTokenObj()); unset($claims['iat']); diff --git a/src/Traits/CommonTrait.php b/src/Traits/CommonTrait.php index 6aa3577..4ac62c3 100644 --- a/src/Traits/CommonTrait.php +++ b/src/Traits/CommonTrait.php @@ -166,7 +166,7 @@ public function verifyToken(Token $token) { $alg = $token->getHeader('alg'); if (empty($this->supportedAlgs[$alg])) { - throw new TokenValidException('Algorithm not supported', 500); + throw new TokenValidException('Algorithm not supported', 401); } /** @var Signer $signer */ $signer = new $this->supportedAlgs[$alg]; @@ -210,7 +210,7 @@ public function getHeaderToken() return $token; } - throw new JWTException('A token is required', 500); + throw new JWTException('A token is required', 400); } /**