JWT“签名验证失败”与PHP

Abu*_*aad 5 php jwt

我在 php 中使用 Firebase/JWT。我正在尝试读取“已解码”php 文件中的令牌,但它显示签名验证失败,不知道为什么会发生这种情况。这就是我编码令牌的方式

<?php 
use \Firebase\JWT\JWT;
require 'vendor/autoload.php';

require('config/Database.php');
$db = new Database;

$key = "helloworld";


//$jwt = JWT::encode($token, $key, 'HS512');

$post = file_get_contents("php://input");
$postdata = json_decode($post);

if($postdata){

    $email = $postdata->email;
    $password = $postdata->password;

    $query = "SELECT * FROM users WHERE email = :email";
    $db->query($query);
    $db->bind(":email", $email);
    $rows = $db->resultset();

    if(password_verify($password, $rows[0]["hash"])){
        $rows[0]["Success"] = "Success";
        $token = array(
            "rows" => $rows
        );
        $jwt = JWT::encode($token, $key, 'HS256');
        header("auth: " . $jwt);
        echo json_encode($jwt, 128);
    }else{
        echo "Failed";
    }
}


?>
Run Code Online (Sandbox Code Playgroud)

然后我正在解码这个文件中的令牌

<?php 

use \Firebase\JWT\JWT;
require 'vendor/autoload.php';

require('config/Database.php');
$db = new Database;

$key = "helloworld";


//$jwt = JWT::encode($token, $key, 'HS512');

$post = file_get_contents("php://input");
$postdata = json_decode($post);


if($postdata){
    $userData = $postdata->userdata;
    // check if token is same stored in the database then decode
    $jwt = JWT::decode($userData, $key, array('HS256'));

    echo $jwt;
}
?>
Run Code Online (Sandbox Code Playgroud)

它失败,返回“签名验证失败”错误。任何帮助表示赞赏。谢谢你。

Abu*_*aad 6

它没有验证的问题是因为我在 jwt 上使用 json_encode ,这导致它再次被引号包围,所以令牌看起来像这样“”eY0lkajflajk ....”,这导致了验证异常。谢谢@zerkms 提出这个问题。