簡體   English   中英

使用PHP進行JWT“簽名驗證失敗”

[英]JWT “Signature verification failed” with PHP

我正在使用帶有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";
    }
}


?>

然后我正在解碼此文件中的令牌

<?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並且導致它再次被引號括起來所以令牌看起來像這樣的“”eY0lkajflajk ....“”並且這導致了驗證異常。 謝謝@zerkms帶來的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM