我将 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/