【问题标题】:Firebase JWT: Signature verification failedFirebase JWT:签名验证失败
【发布时间】:2016-06-12 22:11:41
【问题描述】:

我正在尝试对 Firebase 使用 JWT 身份验证,但我总是收到以下错误:“致命错误:未捕获的 Firebase\JWT\SignatureInvalidException:签名验证失败”。

代码是这样的:

$key = "test";

$tokenId    = base64_encode(mcrypt_create_iv(32));
$issuedAt   = time();
$notBefore  = $issuedAt + 10;
$expire     = $notBefore + 60;
$serverName = $_SERVER["SERVER_NAME"];


$data = [
    'iat'  => $issuedAt,
    'jti'  => $tokenId,
    'iss'  => $serverName,
    'nbf'  => $notBefore,
    'exp'  => $expire,
    "userId"  => 1
];


$secretKey = base64_decode($key);


$jwt = \Firebase\JWT\JWT::encode($data, $secretKey, 'HS256');

// and when I decode the tokens, I got that exception
$decoded = \Firebase\JWT\JWT::decode($jwt, $key, array('HS256'));

我做错了什么?

【问题讨论】:

    标签: php jwt firebase-authentication


    【解决方案1】:

    您不需要 $secretKey 或 base64_decode 密钥,只需这样做:

    $jwt = \Firebase\JWT\JWT::encode($data, $key, 'HS256');
    $decoded = \Firebase\JWT\JWT::decode($jwt, $key, array('HS256'));
    

    【讨论】:

    • 顺便问一个问题(可能与此问题无关,但我请您回答):每当令牌过期/标头中缺少令牌/令牌无效时,我都会发送 401 状态代码。所以使用我的 API 的人不知道如何处理这个问题。例如,如果 401 是由于令牌相关问题或其他原因引起的,他无法发挥作用。我们应该如何处理?
    猜你喜欢
    • 2018-04-30
    • 2020-12-04
    • 2018-10-02
    • 2016-12-27
    • 2019-06-14
    • 2016-12-16
    • 2018-11-03
    • 2021-09-10
    • 1970-01-01
    相关资源
    最近更新 更多