生成JWT格式的token

JWT简介

  • JWT是json web token缩写
  • 它将用户信息加密到token里,服务器不保存任何用户信息
  • 服务器通过使用保存的密钥验证token的正确性,只要正确即通过验证
  • 于token的身份验证可以替代传统的cookie+session身份验证方法


JWT组成

header部分

{
  "alg": "HS256",
  "typ": "JWT"
}
//对应base64UrlEncode编码为:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
//说明:该字段为json格式。alg字段指定了生成signature的算法,默认值为HS256,typ默认值为JWT

payload部分

{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
}
//对应base64UrlEncode编码为:eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
//说明:该字段为json格式,表明用户身份的数据,可以自己自定义字段,很灵活。sub 面向的用户,name 姓名 ,iat 签发时间。例如可自定义示例如下:
{
    "iss": "admin",          //该JWT的签发者
    "iat": 1535967430,        //签发时间
    "exp": 1535974630,        //过期时间
    "nbf": 1535967430,         //该时间之前不接收处理该Token
    "sub": "www.admin.com",   //面向的用户
    "jti": "9f10e796726e332cec401c569969e13e"   //该Token唯一标识
}

signature部分

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  123456
) 
//对应的签名为:keH6T3x1z7mmhKL1T3r9sQdAxxdzB6siemGMr_6ZOwU

//最终得到的JWT的json为(header.payload.signature):eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.keH6T3x1z7mmhKL1T3r9sQdAxxdzB6siemGMr_6ZOwU
//说明:对header和payload进行base64UrlEncode编码后进行拼接。通过key(这里是123456)进行HS256算法签名。

JWT使用流程

  • 初次登录:用户初次登录,输入用户名密码
  • 密码验证:服务器从数据库取出用户名和密码进行验证
  • 生成JWT:服务器端验证通过,根据从数据库返回的信息,以及预设规则,生成JWT
  • 返还JWT:服务器的HTTP RESPONSE中将JWT返还
  • 带JWT的请求:以后客户端发起请求,HTTP REQUEST
  • HEADER中的Authorizatio字段都要有值,为JWT
  • 服务器验证JWT
 'HS256', 'typ' => 'JWT'];

    //使用HMAC生成信息摘要时所使用的密钥
    private static $key = '123456';

    /**
     * 获取jwt token
     * @param array $payload jwt载荷   格式如下非必须
     * [
     *  'iss'=>'jwt_admin',  //该JWT的签发者
     *  'iat'=>time(),  //签发时间
     *  'exp'=>time()+7200,  //过期时间
     *  'nbf'=>time()+60,  //该时间之前不接收处理该Token
     *  'sub'=>'www.admin.com',  //面向的用户
     *  'jti'=>md5(uniqid('JWT').time())  //该Token唯一标识
     * ]
     * @return bool|string
     */
    public static function getToken(array $payload)
    {
        if (is_array($payload)) {
            echo "开始生成token\n
"; $base64header = self::base64UrlEncode(json_encode(self::$header, JSON_UNESCAPED_UNICODE)); echo "header部分\n"; echo "$base64header
"; $base64payload = self::base64UrlEncode(json_encode($payload, JSON_UNESCAPED_UNICODE)); echo "payload部分\n"; echo "$base64payload
"; $token = $base64header . '.' . $base64payload . '.' . self::signature($base64header . '.' . $base64payload, self::$key, self::$header['alg']); echo "token整体\n"; echo "$token
"; return $token; } else { return false; } } /** * 验证token是否有效,默认验证exp,nbf,iat时间 * @param string $Token 需要验证的token * @return bool|string */ public static function verifyToken(string $Token) { echo "开始对签名验证\n
"; $tokens = explode('.', $Token); echo "得到header payload 及第三部分\n"; var_dump($tokens); if (count($tokens) != 3) return false; list($base64header, $base64payload, $sign) = $tokens; //获取jwt算法 $base64decodeheader = json_decode(self::base64UrlDecode($base64header), JSON_OBJECT_AS_ARRAY); if (empty($base64decodeheader['alg'])) return false; //签名验证 if (self::signature($base64header . '.' . $base64payload, self::$key, $base64decodeheader['alg']) !== $sign) return false; $payload = json_decode(self::base64UrlDecode($base64payload), JSON_OBJECT_AS_ARRAY); //签发时间大于当前服务器时间验证失败 if (isset($payload['iat']) && $payload['iat'] > time()) return false; //过期时间小宇当前服务器时间验证失败 if (isset($payload['exp']) && $payload['exp'] < time()) return false; //该nbf时间之前不接收处理该Token if (isset($payload['nbf']) && $payload['nbf'] > time()) return false; return $payload; } /** * base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现 * @param string $input 需要编码的字符串 * @return string */ private static function base64UrlEncode(string $input) { return str_replace('=', '', strtr(base64_encode($input), '+/', '-_')); } /** * base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现 * @param string $input 需要解码的字符串 * @return bool|string */ private static function base64UrlDecode(string $input) { $remainder = strlen($input) % 4; if ($remainder) { $addlen = 4 - $remainder; $input .= str_repeat('=', $addlen); } return base64_decode(strtr($input, '-_', '+/')); } /** * HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现 * @param string $input 为base64UrlEncode(header).".".base64UrlEncode(payload) * @param string $key * @param string $alg 算法方式 * @return mixed */ private static function signature(string $input, string $key, string $alg = 'HS256') { $alg_config = ['HS256' => 'sha256']; return self::base64UrlEncode(hash_hmac($alg_config[$alg], $input, $key, true)); } } //测试和官网是否匹配begin //$payload = ['sub' => '1234567890', 'name' => 'John Doe', 'iat' => 1516239022]; //$jwt = new Jwt; //echo "
";
//$token = $jwt->getToken($payload);
//
//对token进行验证签名
//$getPayload = $jwt->verifyToken($token);
//echo "

"; //var_dump($getPayload); //echo "

"; //测试和官网是否匹配end //自己使用测试begin $payload_test = [ 'iss' => 'admin', 'iat' => time(), 'exp' => time() + 7200, 'nbf' => time(), 'sub' => 'www.admin.com', 'jti' => md5(uniqid('JWT') . time()), 'mtoken' => '0854089c8fa538ab3dd3d2e311b32c92', 'btoken' => '50329e02db492b461120c25a212413c2', 'utoken' => '8f5a5312acb94c5885f932795c710922' ]; $token_test = Jwt::getToken($payload_test); echo "
";
//echo $token_test;

//对token进行验证签名
$getPayload_test = Jwt::verifyToken($token_test);
echo "

"; var_dump($getPayload_test); echo "

"; //自己使用时候end
开始生成token
header部分 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
payload部分 eyJpc3MiOiJhZG1pbiIsImlhdCI6MTU0ODEyNjkyMCwiZXhwIjoxNTQ4MTM0MTIwLCJuYmYiOjE1NDgxMjY5MjAsInN1YiI6Ind3dy5hZG1pbi5jb20iLCJqdGkiOiJkYjYyZDMxZmEyNzJiY2JhM2JkNzYzYjUzY2I3MjRlYyIsIm10b2tlbiI6IjA4NTQwODljOGZhNTM4YWIzZGQzZDJlMzExYjMyYzkyIiwiYnRva2VuIjoiNTAzMjllMDJkYjQ5MmI0NjExMjBjMjVhMjEyNDEzYzIiLCJ1dG9rZW4iOiI4ZjVhNTMxMmFjYjk0YzU4ODVmOTMyNzk1YzcxMDkyMiJ9
token整体 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTU0ODEyNjkyMCwiZXhwIjoxNTQ4MTM0MTIwLCJuYmYiOjE1NDgxMjY5MjAsInN1YiI6Ind3dy5hZG1pbi5jb20iLCJqdGkiOiJkYjYyZDMxZmEyNzJiY2JhM2JkNzYzYjUzY2I3MjRlYyIsIm10b2tlbiI6IjA4NTQwODljOGZhNTM4YWIzZGQzZDJlMzExYjMyYzkyIiwiYnRva2VuIjoiNTAzMjllMDJkYjQ5MmI0NjExMjBjMjVhMjEyNDEzYzIiLCJ1dG9rZW4iOiI4ZjVhNTMxMmFjYjk0YzU4ODVmOTMyNzk1YzcxMDkyMiJ9.iUCC9QSk_HlRCvm8B7BAyOyxpgwCa-OwAhhbvHMRpyk
开始对签名验证
得到header payload 及第三部分
D:\WebServer\www\jwtDemo\Jwt.php:57:
array (size=3)
  0 => string 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' (length=36)
  1 => string 'eyJpc3MiOiJhZG1pbiIsImlhdCI6MTU0ODEyNjkyMCwiZXhwIjoxNTQ4MTM0MTIwLCJuYmYiOjE1NDgxMjY5MjAsInN1YiI6Ind3dy5hZG1pbi5jb20iLCJqdGkiOiJkYjYyZDMxZmEyNzJiY2JhM2JkNzYzYjUzY2I3MjRlYyIsIm10b2tlbiI6IjA4NTQwODljOGZhNTM4YWIzZGQzZDJlMzExYjMyYzkyIiwiYnRva2VuIjoiNTAzMjllMDJkYjQ5MmI0NjExMjBjMjVhMjEyNDEzYzIiLCJ1dG9rZW4iOiI4ZjVhNTMxMmFjYjk0YzU4ODVmOTMyNzk1YzcxMDkyMiJ9' (length=348)
  2 => string 'iUCC9QSk_HlRCvm8B7BAyOyxpgwCa-OwAhhbvHMRpyk' (length=43)


D:\WebServer\www\jwtDemo\Jwt.php:161:
array (size=9)
  'iss' => string 'admin' (length=5)
  'iat' => int 1548126920
  'exp' => int 1548134120
  'nbf' => int 1548126920
  'sub' => string 'www.admin.com' (length=13)
  'jti' => string 'db62d31fa272bcba3bd763b53cb724ec' (length=32)
  'mtoken' => string '0854089c8fa538ab3dd3d2e311b32c92' (length=32)
  'btoken' => string '50329e02db492b461120c25a212413c2' (length=32)
  'utoken' => string '8f5a5312acb94c5885f932795c710922' (length=32)

 

你可能感兴趣的:(权限管理)