我在尝试使用 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;
}
?>
Run Code Online (Sandbox Code Playgroud)
JWT.php 中不允许使用算法:97
有人经历过这个吗?
保罗
如果我没记错的话,在 JWT 的最新版本中,该decode函数需要一个新参数(允许的算法)。以前不需要这样做。
我用它来获取所有支持的算法:
$decoded = JWT::decode($token, $key, array_keys(JWT::$supported_algs));
Run Code Online (Sandbox Code Playgroud)
小智 0
您应该使用谷歌密钥来解码令牌。
$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"));
Run Code Online (Sandbox Code Playgroud)
另外,由于服务器时区,我遇到了问题,因此我在解码行之前添加了以下内容:
JWT::$leeway = 600;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5692 次 |
| 最近记录: |