jwt 校验抛出异常改为返回false
最新推荐文章于 2023-11-03 15:41:13 发布
public static function setToken($account_id, $privateKey) { $signer = new Sha256(); $keychain = new Keychain(); $token = (new Builder()) ->setExpiration(time() + 3600) ->set('account_id', $account_id) ->sign($signer, $keychain->getPrivateKey(RSA::formatPrivateKey("{$privateKey}"))) ->getToken(); return (string) $token; } public static function getTokenInfo($token, $publicKey) { $signer = new Sha256(); $keychain = new Keychain();
这样可以实现如果抛出异常时返回false
try { $token = (new Parser())->parse((string) $token); } catch (\Exception $e) { return false; } if(!@$token->verify($signer, @$keychain->getPublicKey(RSA::formatPublicKey("{$publicKey}")))) return false; if(@$token->getClaim('exp') > time()){ return @$token->getClaim('account_id'); } return false; } }