JWT无效签名-PHP

时间:2019-01-14 06:47:20

标签: php jwt

我正在使用“ Lcobucci”库创建JWT。 运行代码时,发生无效的签名错误。 我搜索了很多教程,仍然没有弄清楚错误。 这是代码:

require "vendor/autoload.php"; // Autoload.php is generated by Composer

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;

$GLOBALS['ApiKey'] = '6e845674-96b3-4e85-b843-418ceae4491f';
$GLOBALS['ApiId'] = '5c38c665ff626b1fe01a3987';
$GLOBALS['OrgUnitId'] = '5c33bddeff626b1fe015635e';

$_SESSION['TransactionId'] = '123456';

$_SESSION['Order'] = array(
    "OrderDetails" => array(
        "OrderNumber" =>  "100000425",
        "Amount" => '1500',
        "CurrencyCode" => '840'
        )
);

function generateJwt($orderTransactionId, $orderObj){

    $currentTime = time();
    $expireTime = 3600; 

    $token = (new Builder())->setIssuer($GLOBALS['ApiId']) 
                ->setId($orderTransactionId, true) 
                ->setIssuedAt($currentTime)
                ->setExpiration($currentTime + $expireTime)
                ->set('OrgUnitId', $GLOBALS['OrgUnitId'])
                ->set('Payload', $_SESSION['Order']) 
                ->set('ObjectifyPayload', true)
                ->sign(new Sha256(), $GLOBALS['ApiKey']) 
                ->getToken();   
    return $token; // The JWT String
}

echo generateJwt($_SESSION['TransactionId'], $_SESSION['Order']);

我在jwt.io中引用了令牌 有什么帮助吗? 请参阅屏幕截图。JWT Refrence

1 个答案:

答案 0 :(得分:0)

您的私钥应为pkcs1格式。有关示例,请参见此测试代码:

https://github.com/lcobucci/jwt/blob/master/test/unit/Signer/RsaTest.php

或者如果您尝试执行hs256,则密钥中包含破折号。拿出来。