尝试使用php和Firebase解密JWT令牌时遇到很多问题
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'vendor/autoload.php';
use \Firebase\JWT\JWT;
try {
$key = 'MYKEY';
$token = $_POST['id_token'];
$data = JWT::decode($token, $key, array('HS256'));
} catch (\Exception $e) {
echo $e;
}
?>
JWT.php:97中不允许使用算法
有人有过吗?
保罗
答案 0 :(得分:0)
如果我没记错的话,在最近版本的 JWT 中,decode
函数需要一个新参数(允许的算法)。以前这不是必需的。
我用它来获取所有支持的算法:
$decoded = JWT::decode($token, $key, array_keys(JWT::$supported_algs));
答案 1 :(得分:-1)
您应该使用谷歌密钥来解码令牌。
$googleKeysURL = 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com';
$key = json_decode(file_get_contents($googleKeysURL), true);
$decoded = JWT::decode($token, $key, array("RS256"));
由于服务器时区的原因,我也遇到了问题,所以我在解码行之前添加了这个:
JWT::$leeway = 600;