????PHP JWT(JSON Web Token)????,?????????:
- ??UUID??????????token ID:???JWT?,????????UUID????????ID?????????????token????????
// ????ID function generateUniqueId() { return sprintf('XX-X-X-X-XXX', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); }
- ?JWT???????ID:??????ID??JWT??????,??????token???????????
$uniqueId = generateUniqueId(); $payload = [ 'iss' => 'your_issuer', 'aud' => 'your_audience', 'iat' => time(), 'nbf' => time() + 10, 'exp' => time() + 3600, 'jti' => $uniqueId // ????ID?JWT?? ];
- ???????????:?????????(?HS256?RS256)??????????JWT????????????token???????,?????????
$key = 'your_secret_key'; $header = base64_encode(json_encode([ 'alg' => 'HS256', 'typ' => 'JWT' ])); $payload = base64_encode(json_encode($payload)); $signature = hash_hmac('sha256', $header . '.' . $payload, $key, true); $jwt = $header . '.' . $payload . '.' . base64_encode($signature);
????????,?????PHP JWT????,???????????????