💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# jwt ``` <pre class="calibre15">``` <span class="token"><</span><span class="token">?</span>php <span class="token6">/** * PHP实现jwt */</span> class <span class="token3">Jwt</span> <span class="token1">{</span> <span class="token6">//头部</span> private static $header<span class="token">=</span><span class="token3">array</span><span class="token1">(</span> <span class="token4">'alg'</span><span class="token">=</span><span class="token">></span><span class="token4">'HS256'</span><span class="token1">,</span> <span class="token6">//生成signature的算法</span> <span class="token4">'typ'</span><span class="token">=</span><span class="token">></span><span class="token4">'JWT'</span> <span class="token6">//类型</span> <span class="token1">)</span><span class="token1">;</span> <span class="token6">//使用HMAC生成信息摘要时所使用的密钥</span> private static $key<span class="token">=</span><span class="token4">'123456'</span><span class="token1">;</span> <span class="token6">/** * 获取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 */</span> public static <span class="token2">function</span> <span class="token3">getToken</span><span class="token1">(</span>array $payload<span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span><span class="token3">is_array</span><span class="token1">(</span>$payload<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> $base64header<span class="token">=</span>self<span class="token1">:</span><span class="token1">:</span><span class="token3">base64UrlEncode</span><span class="token1">(</span><span class="token3">json_encode</span><span class="token1">(</span>self<span class="token1">:</span><span class="token1">:</span>$header<span class="token1">,</span>JSON_UNESCAPED_UNICODE<span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> $base64payload<span class="token">=</span>self<span class="token1">:</span><span class="token1">:</span><span class="token3">base64UrlEncode</span><span class="token1">(</span><span class="token3">json_encode</span><span class="token1">(</span>$payload<span class="token1">,</span>JSON_UNESCAPED_UNICODE<span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> $token<span class="token">=</span>$base64header<span class="token1">.</span><span class="token4">'.'</span><span class="token1">.</span>$base64payload<span class="token1">.</span><span class="token4">'.'</span><span class="token1">.</span>self<span class="token1">:</span><span class="token1">:</span><span class="token3">signature</span><span class="token1">(</span>$base64header<span class="token1">.</span><span class="token4">'.'</span><span class="token1">.</span>$base64payload<span class="token1">,</span>self<span class="token1">:</span><span class="token1">:</span>$key<span class="token1">,</span>self<span class="token1">:</span><span class="token1">:</span>$header<span class="token1">[</span><span class="token4">'alg'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> $token<span class="token1">;</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token6">/** * 验证token是否有效,默认验证exp,nbf,iat时间 * @param string $Token 需要验证的token * @return bool|string */</span> public static <span class="token2">function</span> <span class="token3">verifyToken</span><span class="token1">(</span>string $Token<span class="token1">)</span> <span class="token1">{</span> $tokens <span class="token">=</span> <span class="token3">explode</span><span class="token1">(</span><span class="token4">'.'</span><span class="token1">,</span> $Token<span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">count</span><span class="token1">(</span>$tokens<span class="token1">)</span> <span class="token">!=</span> <span class="token5">3</span><span class="token1">)</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token3">list</span><span class="token1">(</span>$base64header<span class="token1">,</span> $base64payload<span class="token1">,</span> $sign<span class="token1">)</span> <span class="token">=</span> $tokens<span class="token1">;</span> <span class="token6">//获取jwt算法</span> $base64decodeheader <span class="token">=</span> <span class="token3">json_decode</span><span class="token1">(</span>self<span class="token1">:</span><span class="token1">:</span><span class="token3">base64UrlDecode</span><span class="token1">(</span>$base64header<span class="token1">)</span><span class="token1">,</span> JSON_OBJECT_AS_ARRAY<span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$base64decodeheader<span class="token1">[</span><span class="token4">'alg'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">)</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token6">//签名验证</span> <span class="token2">if</span> <span class="token1">(</span>self<span class="token1">:</span><span class="token1">:</span><span class="token3">signature</span><span class="token1">(</span>$base64header <span class="token1">.</span> <span class="token4">'.'</span> <span class="token1">.</span> $base64payload<span class="token1">,</span> self<span class="token1">:</span><span class="token1">:</span>$key<span class="token1">,</span> $base64decodeheader<span class="token1">[</span><span class="token4">'alg'</span><span class="token1">]</span><span class="token1">)</span> <span class="token">!==</span> $sign<span class="token1">)</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> $payload <span class="token">=</span> <span class="token3">json_decode</span><span class="token1">(</span>self<span class="token1">:</span><span class="token1">:</span><span class="token3">base64UrlDecode</span><span class="token1">(</span>$base64payload<span class="token1">)</span><span class="token1">,</span> JSON_OBJECT_AS_ARRAY<span class="token1">)</span><span class="token1">;</span> <span class="token6">//签发时间大于当前服务器时间验证失败</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">isset</span><span class="token1">(</span>$payload<span class="token1">[</span><span class="token4">'iat'</span><span class="token1">]</span><span class="token1">)</span> <span class="token">&&</span> $payload<span class="token1">[</span><span class="token4">'iat'</span><span class="token1">]</span> <span class="token">></span> <span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token6">//过期时间小宇当前服务器时间验证失败</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">isset</span><span class="token1">(</span>$payload<span class="token1">[</span><span class="token4">'exp'</span><span class="token1">]</span><span class="token1">)</span> <span class="token">&&</span> $payload<span class="token1">[</span><span class="token4">'exp'</span><span class="token1">]</span> <span class="token"><</span> <span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token6">//该nbf时间之前不接收处理该Token</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">isset</span><span class="token1">(</span>$payload<span class="token1">[</span><span class="token4">'nbf'</span><span class="token1">]</span><span class="token1">)</span> <span class="token">&&</span> $payload<span class="token1">[</span><span class="token4">'nbf'</span><span class="token1">]</span> <span class="token">></span> <span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token2">return</span> $payload<span class="token1">;</span> <span class="token1">}</span> <span class="token6">/** * base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现 * @param string $input 需要编码的字符串 * @return string */</span> private static <span class="token2">function</span> <span class="token3">base64UrlEncode</span><span class="token1">(</span>string $input<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token3">str_replace</span><span class="token1">(</span><span class="token4">'='</span><span class="token1">,</span> <span class="token4">''</span><span class="token1">,</span> <span class="token3">strtr</span><span class="token1">(</span><span class="token3">base64_encode</span><span class="token1">(</span>$input<span class="token1">)</span><span class="token1">,</span> <span class="token4">'+/'</span><span class="token1">,</span> <span class="token4">'-_'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token6">/** * base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现 * @param string $input 需要解码的字符串 * @return bool|string */</span> private static <span class="token2">function</span> <span class="token3">base64UrlDecode</span><span class="token1">(</span>string $input<span class="token1">)</span> <span class="token1">{</span> $remainder <span class="token">=</span> <span class="token3">strlen</span><span class="token1">(</span>$input<span class="token1">)</span> <span class="token">%</span> <span class="token5">4</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$remainder<span class="token1">)</span> <span class="token1">{</span> $addlen <span class="token">=</span> <span class="token5">4</span> <span class="token">-</span> $remainder<span class="token1">;</span> $input <span class="token1">.</span><span class="token">=</span> <span class="token3">str_repeat</span><span class="token1">(</span><span class="token4">'='</span><span class="token1">,</span> $addlen<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">return</span> <span class="token3">base64_decode</span><span class="token1">(</span><span class="token3">strtr</span><span class="token1">(</span>$input<span class="token1">,</span> <span class="token4">'-_'</span><span class="token1">,</span> <span class="token4">'+/'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token6">/** * HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现 * @param string $input 为base64UrlEncode(header).".".base64UrlEncode(payload) * @param string $key * @param string $alg 算法方式 * @return mixed */</span> private static <span class="token2">function</span> <span class="token3">signature</span><span class="token1">(</span>string $input<span class="token1">,</span> string $key<span class="token1">,</span> string $alg <span class="token">=</span> <span class="token4">'HS256'</span><span class="token1">)</span> <span class="token1">{</span> $alg_config<span class="token">=</span><span class="token3">array</span><span class="token1">(</span> <span class="token4">'HS256'</span><span class="token">=</span><span class="token">></span><span class="token4">'sha256'</span> <span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> self<span class="token1">:</span><span class="token1">:</span><span class="token3">base64UrlEncode</span><span class="token1">(</span><span class="token3">hash_hmac</span><span class="token1">(</span>$alg_config<span class="token1">[</span>$alg<span class="token1">]</span><span class="token1">,</span> $input<span class="token1">,</span> $key<span class="token1">,</span><span class="token5">true</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token6">//测试和官网是否匹配begin</span> $payload<span class="token">=</span><span class="token3">array</span><span class="token1">(</span><span class="token4">'sub'</span><span class="token">=</span><span class="token">></span><span class="token4">'1234567890'</span><span class="token1">,</span><span class="token4">'name'</span><span class="token">=</span><span class="token">></span><span class="token4">'John Doe'</span><span class="token1">,</span><span class="token4">'iat'</span><span class="token">=</span><span class="token">></span><span class="token5">1516239022</span><span class="token1">)</span><span class="token1">;</span> $jwt<span class="token">=</span><span class="token2">new</span> <span class="token3">Jwt</span><span class="token1">;</span> $token<span class="token">=</span>$jwt<span class="token">-</span><span class="token">></span><span class="token3">getToken</span><span class="token1">(</span>$payload<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">""</span><span class="token1">;</span> echo $token<span class="token1">;</span> <span class="token6">//对token进行验证签名</span> $getPayload<span class="token">=</span>$jwt<span class="token">-</span><span class="token">></span><span class="token3">verifyToken</span><span class="token1">(</span>$token<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">"<br><br>"</span><span class="token1">;</span> <span class="token3">var_dump</span><span class="token1">(</span>$getPayload<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">"<br><br>"</span><span class="token1">;</span> <span class="token6">//测试和官网是否匹配end</span> <span class="token6">//自己使用测试begin</span> $payload_test<span class="token">=</span><span class="token3">array</span><span class="token1">(</span><span class="token4">'iss'</span><span class="token">=</span><span class="token">></span><span class="token4">'admin'</span><span class="token1">,</span><span class="token4">'iat'</span><span class="token">=</span><span class="token">></span><span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'exp'</span><span class="token">=</span><span class="token">></span><span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token">+</span><span class="token5">7200</span><span class="token1">,</span><span class="token4">'nbf'</span><span class="token">=</span><span class="token">></span><span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">,</span><span class="token4">'sub'</span><span class="token">=</span><span class="token">></span><span class="token4">'www.admin.com'</span><span class="token1">,</span><span class="token4">'jti'</span><span class="token">=</span><span class="token">></span><span class="token3">md5</span><span class="token1">(</span><span class="token3">uniqid</span><span class="token1">(</span><span class="token4">'JWT'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span><span class="token1">;</span> $token_test<span class="token">=</span>Jwt<span class="token1">:</span><span class="token1">:</span><span class="token3">getToken</span><span class="token1">(</span>$payload_test<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">""</span><span class="token1">;</span> echo $token_test<span class="token1">;</span> <span class="token6">//对token进行验证签名</span> $getPayload_test<span class="token">=</span>Jwt<span class="token1">:</span><span class="token1">:</span><span class="token3">verifyToken</span><span class="token1">(</span>$token_test<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">"<br><br>"</span><span class="token1">;</span> <span class="token3">var_dump</span><span class="token1">(</span>$getPayload_test<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">"<br><br>"</span><span class="token1">;</span> <span class="token6">//自己使用时候end</span> ``` ```