PHP JWT 令牌签名无效
PHP JWT Token Invalid Signature
我现在搜索了一个小时,但找不到解决此问题的方法。
这是生成 JWT 令牌的代码。我使用了 https://github.com/firebase/php-jwt 库。
$tokenId = base64_encode(mcrypt_create_iv(32));
$issuedAt = time();
$notBefore = $issuedAt + 10; //Adding 10 seconds
$expire = $notBefore + 60; // Adding 60 seconds
$serverName = 'serverName'; // Retrieve the server name from config file
$secretKey = base64_decode(getenv('JWT_SECRET'));
$data = [
'iat' => $issuedAt, // Issued at: time when the token was generated
'jti' => $tokenId, // Json Token Id: an unique identifier for the token
'iss' => $serverName, // Issuer
'nbf' => $notBefore, // Not before
'exp' => $expire, // Expire
'data' => [ // Data related to the signer user
'userId' => '1', // userid from the users table
'userName' => $UserName, // User name
]
];
$jwt = JWT::encode(
$data, //Data to be encoded in the JWT
$secretKey, // The signing key
'HS256' // Algorithm used to sign the token
);
$unencodedArray = ['jwt' => $jwt];
echo json_encode($unencodedArray);
然后我在 https://jwt.io/
验证令牌
谁能帮我解决这个问题?我目前是 JWT 的新手。顺便说一句,我的项目是 Slim API.
非常感谢。
签名验证失败,因为您没有将正确的密钥传递给 https://jwt.io/ 您需要从 PHP 代码传递 $secretKey
的值。根据屏幕截图,您传递的是字符串 secret
.
我现在搜索了一个小时,但找不到解决此问题的方法。
这是生成 JWT 令牌的代码。我使用了 https://github.com/firebase/php-jwt 库。
$tokenId = base64_encode(mcrypt_create_iv(32));
$issuedAt = time();
$notBefore = $issuedAt + 10; //Adding 10 seconds
$expire = $notBefore + 60; // Adding 60 seconds
$serverName = 'serverName'; // Retrieve the server name from config file
$secretKey = base64_decode(getenv('JWT_SECRET'));
$data = [
'iat' => $issuedAt, // Issued at: time when the token was generated
'jti' => $tokenId, // Json Token Id: an unique identifier for the token
'iss' => $serverName, // Issuer
'nbf' => $notBefore, // Not before
'exp' => $expire, // Expire
'data' => [ // Data related to the signer user
'userId' => '1', // userid from the users table
'userName' => $UserName, // User name
]
];
$jwt = JWT::encode(
$data, //Data to be encoded in the JWT
$secretKey, // The signing key
'HS256' // Algorithm used to sign the token
);
$unencodedArray = ['jwt' => $jwt];
echo json_encode($unencodedArray);
然后我在 https://jwt.io/
验证令牌谁能帮我解决这个问题?我目前是 JWT 的新手。顺便说一句,我的项目是 Slim API.
非常感谢。
签名验证失败,因为您没有将正确的密钥传递给 https://jwt.io/ 您需要从 PHP 代码传递 $secretKey
的值。根据屏幕截图,您传递的是字符串 secret
.