php - JWT "Signature verification failed"与 PHP

标签 php jwt

我将 Firebase/JWT 与 php 结合使用。我正在尝试读取“已解码”php 文件中的 token ,但它显示签名验证失败,不确定为什么会这样。这就是我编码 token 的方式

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


?>

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

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

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

最佳答案

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

关于php - JWT "Signature verification failed"与 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859475/

相关文章:

php - 在 PHP 7 Ubuntu 14.04 上调用未定义函数 mb_strlen()

php - 如何用 PHP 替换字符串中的变量?

php - 如何更改 WP-API 的 JWT 身份验证上的 JSON 响应?

Node.js JWT token

authentication - K6 - 身份验证 - 获取授权 token

authentication - UseJwtBearerAuthentication 没有填充 User.Identity.Name

php - 从 php 执行 python

PHP 与 array_filter 相反?

node.js - JWT(Json Web Token)的过期时间保存在哪里?

php - 使用 Livewire 仅选中一个复选框并取消选中其他复选框