三种方法实现jwt
方法一:
1.引入php-jwt包
composer require firebase/php-jwt
2.env
[TOKEN] KEY = yyp iss = http://admin.week3.com/ aud = zwx
3.htaccess
RewriteCond %{HTTP:Authorization} ^(.+)$ RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
4.代码
控制器文件:app\api\controller\Jwt.php
<?php
namespace app\api\lib;
use app\BaseController;
use Firebase\JWT\ExpiredException;
use Firebase\JWT\JWT as JWTUtil;
use think\facade\Request;
class Jwt
{
/**
* 根据json web token设置的规则生成token
* @return \think\response\Json
*/
public function createjwt($userid=null)
{
//jwt的签发密钥,验证token的时候需要用到
$key = md5(env('TOKEN.KEY','pyg'));
//签发时间
$time = time();
//过期时间
$expire = $time + 14400;
$token = array(
//暂时固定
"user_id" => $userid,
//签发组织
"iss" => env('TOKEN.iss',''),
//签发作者
"aud" => env('TOKEN.aud',''),
//颁发时间
"iat" => $tim