PHP code example of bang / jwt
1. Go to this page and download the library: Download bang/jwt library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bang / jwt example snippets
use bang\jwt\Jwt;
class Index
{
public function test()
{
//setSecretKey是设置Secret Key 的方法
$jwtObject = Jwt::getInstance()->setSecretKey('xowuf')->publish();
$jwtObject->setAlg('HMACSHA256'); // 加密方式
$jwtObject->setAud('user'); // 用户
$jwtObject->setExp(time()+3600); // 过期时间
$jwtObject->setIat(time()); // 发布时间
$jwtObject->setIss('bang'); // 发行人
$jwtObject->setJti(md5(time())); // jwt id 用于标识该jwt
$jwtObject->setNbf(time()+60*5); // 在此之前不可用
$jwtObject->setSub('主题'); // 主题
// 自定义数据
$jwtObject->setData([
'user_id' => 1
]);
// 最终生成的token
$token = $jwtObject->create();
echo $token;
//解密token
$data = Jwt::getInstance()->decode($token);
var_dump($data);
}
}