安装
使用composer管理依赖项并下载PHP-JWT:
composer require firebase/php-jwt
示例 1
<?php
// 北京时间
date_default_timezone_set("PRC");
require_once "vendor/autoload.php";
use \Firebase\JWT\JWT;
$key = "sdqazxsw2@)!<<+";
$time = time();
$token = [
"iss" => "user_center",// 签发者
"aud" => "full_platform",// 面向的用户
"iat" => $time,// 签发时间
// "nbf" => $time + 5,// 在什么时间之后该jwt才可用
'exp' => $time + 600, // 过期时间-10min
'data' => [// 自定义数据
'user_id' => 1001,
'user_name' => 'xiaoming'
]
];
echo "数据:\n" . print_r($token, true) . "\n";
$jwt = JWT::encode($token, $key);
echo "加密:\n" . print_r($token, true) . "\n";
$decoded = JWT::decode($jwt, $key, ['HS256']);
echo "解密:\n" . print_r($decoded, true) . "\n";
输出
数据:
Array
(
[iss] => user_center
[aud] => full_platform
[iat] => 1551849727
[exp] => 1551850327
[data] => Array
(
[user_id] => 1001
[user_name] => xiaoming
)
)
加密:
Array
(
[iss] => user_center
[aud] => full_platform
[iat] => 1551849727
[exp] => 1551850327
[data] => Array
(
[user_id] => 1001
[user_name] => xiaoming
)
)
解密:
stdClass Object
(
[iss] => user_center
[aud] => full_platform
[iat] => 1551849727
[exp] => 1551850327
[data] => stdClass Object
(
[user_id] => 1001
[user_name] => xiaoming
)
)
示例 2
<?php
require_once "vendor/autoload.php";
use \Firebase\JWT\JWT;
$privateKey = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC8kGa1pSjbSYZVebtTRBLxBz5H4i2p/llLCrEeQhta5kaQu/Rn
vuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t0tyazyZ8JXw+KgXTxldMPEL9
5+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4ehde/zUxo6UvS7UrBQIDAQAB
AoGAb/MXV46XxCFRxNuB8LyAtmLDgi/xRnTAlMHjSACddwkyKem8//8eZtw9fzxz
bWZ/1/doQOuHBGYZU8aDzzj59FZ78dyzNFoF91hbvZKkg+6wGyd/LrGVEB+Xre0J
Nil0GReM2AHDNZUYRv+HYJPIOrB0CRczLQsgFJ8K6aAD6F0CQQDzbpjYdx10qgK1
cP59UHiHjPZYC0loEsk7s+hUmT3QHerAQJMZWC11Qrn2N+ybwwNblDKv+s5qgMQ5
5tNoQ9IfAkEAxkyffU6ythpg/H0Ixe1I2rd0GbF05biIzO/i77Det3n4YsJVlDck
ZkcvY3SK2iRIL4c9yY6hlIhs+K9wXTtGWwJBAO9Dskl48mO7woPR9uD22jDpNSwe
k90OMepTjzSvlhjbfuPN1IdhqvSJTDychRwn1kIJ7LQZgQ8fVz9OCFZ/6qMCQGOb
qaGwHmUK6xzpUbbacnYrIM6nLSkXgOAwv7XXCojvY614ILTK3iXiLBOxPu5Eu13k
eUz9sHyD6vkgZzjtxXECQAkp4Xerf5TGfQXGXhxIX52yH+N2LtujCdkQZjXAsGdm
B2zNzvrlgRmgBrklMTrMYgm1NPcW+bRLGcwgW2PTvNM=
-----END RSA PRIVATE KEY-----
EOD;
$publicKey = <<<EOD
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8kGa1pSjbSYZVebtTRBLxBz5H
4i2p/llLCrEeQhta5kaQu/RnvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t
0tyazyZ8JXw+KgXTxldMPEL95+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4
ehde/zUxo6UvS7UrBQIDAQAB
-----END PUBLIC KEY-----
EOD;
$time = time();
$token = [
"iss" => "user_center",// 签发者
"aud" => "full_platform",// 面向的用户
"iat" => $time,// 签发时间
//"nbf" => $time + 5,// 在什么时间之后该jwt才可用
'exp' => $time + 600, // 过期时间-10min
'data' => [// 自定义数据
'user_id' => 1001,
'user_name' => 'xiaoming'
]
];
echo "原始数据:\n" . print_r($token, true) . "\n";
// 私钥加密
$jwt = JWT::encode($token, $privateKey, 'RS256');
echo "私钥加密:\n" . print_r($jwt, true) . "\n";
// 公钥解密
$decoded = JWT::decode($jwt, $publicKey, ['RS256']);
echo "公钥解密:\n" . print_r($decoded, true) . "\n";
输出
原始数据:
Array
(
[iss] => user_center
[aud] => full_platform
[iat] => 1551850133
[exp] => 1551850733
[data] => Array
(
[user_id] => 1001
[user_name] => xiaoming
)
)
私钥加密:
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ1c2VyX2NlbnRlciIsImF1ZCI6ImZ1bGxfcGxhdGZvcm0iLCJpYXQiOjE1NTE4NTAxMzMsImV4cCI6MTU1MTg1MDczMywiZGF0YSI6eyJ1c2VyX2lkIjoxMDAxLCJ1c2VyX25hbWUiOiJ4aWFvbWluZyJ9fQ.J2S2NN81hH3AyoiTLUfwfUEUz2vjAQz54bWxXHNUVEaODCYK61_m1fnyiDXL9v5Dhv6RZ2OTJ-kp8vMeSSrg3ddbY5MkzOjSh4CoeBGfOKIFPPqfiWtPkOCV6QCHSXg7_vhCIDHjrE-j5qmLMy-ypDkIpIwKO_2760AEZUWIjxc
公钥解密:
stdClass Object
(
[iss] => user_center
[aud] => full_platform
[iat] => 1551850133
[exp] => 1551850733
[data] => stdClass Object
(
[user_id] => 1001
[user_name] => xiaoming
)
)
网友评论