PHP code example of yangchao / php-jwt
1. Go to this page and download the library: Download yangchao/php-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/ */
yangchao / php-jwt example snippets
composer
$config = [
'iss' => 'yangchao/jwt', // 令牌签发者
'signer' => \yangchao\jwt\Config::ALGO_HS256,//加密类型
'nbf' => 5,// 某个时间点后才能访问,单位秒。(如:5 表示当前时间5秒后TOKEN才能使用)
'expires_at' => 5, //过期时间,单位:秒
'refresh_disable' => false,//是否禁用刷新令牌
'refresh_ttl' => 604800,//刷新令牌过期时间,单位:秒
'leeway' => 60, // 容错时间差,单位:秒
'is_single_device' => true, // 是否开启单设备登录
'device_verify' => 'ua', // 单设备验证方式,可选值:ua(User-Agent)、ip(客户端IP)、ip_ua(IP+UA)
'secret_key' => '', //HS256 密钥
'refresh_secret_key' => '',//HS256 刷新密钥
'public_key' => '', //RS256 RSA公钥
'private_key' => '',//RS256 RSA私钥
'refresh_public_key' => '',//RS256 刷新RSA公钥
'refresh_private_key' => '',//RS256 刷新RSA私钥
'black_list'=>[ //黑名单配置
'redis_host' => '120.0.0.1',//黑名单储存 redis主机
'redis_password' => '123456',// redis密码
'redis_port' => 6379,// redis端口
'storage_server'=> XXX::class// 储存服务器类型
],
'admin' => [ //多应用管理配置 同以上配置,如果不配置,则使用默认配置
'iss' => 'jwt-admin', // 令牌签发者
'secret_key' => '', //HS256 密钥
'refresh_secret_key' => '',//HS256 刷新密钥
'public_key' => '', //RS256 RSA公钥
'private_key' => '',//RS256 RSA私钥
'refresh_public_key' => '',//RS256 刷新RSA公钥
'refresh_private_key' => '',//RS256 刷新RSA私钥
]
];
$jwt = new \yangchao\jwt\JWTAuth($config);
//创建token
$token = $jwt->createToken(['a'=>'b']);
//多应用创建token
$token = $jwt->store('admin')->createToken(['a'=>'b']);
//验证token
$claims = $jwt->verifyToken($token)
$claims = $jwt->store('admin')->verifyToken($token)
//刷新Token 当refresh_disable为false时(不禁用刷新),此处token传值为刷新token
$claims = $jwt->refreshToken($token);
$claims = $jwt->store('admin')->refreshToken($token);
//获取token过期时间
$expiresAt = $jwt->getExpireTime();
$expiresAt = $jwt->store('admin')->getExpireTime();
//获取刷新token过期时间
$refreshExpiresAt = $jwt->getRefreshTtlTime();
$refreshExpiresAt = $jwt->store('admin')->getRefreshTtlTime();