jwt 校验抛出异常改为返回false

    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;
    }

}

你可能感兴趣的:(php)