tp 6 JWT

 1.引入php-jwt包


composer require firebase/php-jwt

 2.代码

<?php
namespace app\api\controller;

use Firebase\JWT\ExpiredException;
use Firebase\JWT\JWT as JWTUtil;
use think\Exception;

class Jwt
{
    /**
     * 根据json web token设置的规则生成token
     * @return \think\response\Json
     */
    public static function createjwt($id)
    {
        $key = md5('dd'); //jwt的签发密钥,验证token的时候需要用到
        $time = time(); //签发时间
        $expire = $time + 14400; //过期时间
        $token = array(
            "user_id" => $id,
            "iss" => "http://www.1909A.com/",//签发组织
            "aud" => "lmp", //签发作者
            "iat" => $time,
            "nbf" => $time,
            "exp" => $expire
        );
        $jwt = JWTUtil::encode($token,$key);
        return $jwt;
    }

    /**
     * 验证token
     * @return \think\response\Json
     */
    public static function verifyjwt($jwt)
    {
        $key = md5('dd'); //jwt的签发密钥,验证token的时候需要用到
        try{
            $jwtAuth = json_encode(JWTUtil::decode($jwt,$key,array("HS256")));
            $authInfo = json_decode($jwtAuth,true);
            if (!$authInfo['user_id']){
                throw new Exception("用户不存在");
            }
            return json($authInfo);
        }catch (ExpiredException $e){
            throw new Exception("token过期了");
        }catch (\Exception $e){
            return dump($e->getMessage());
        }

    }

    public static function getRequestToken()
    {
        if (empty($_SERVER['HTTP_AUTHORIZATION'])) {
            return false;
        }
        $header = $_SERVER['HTTP_AUTHORIZATION'];
        $method = 'bearer';
        //去除token中可能存在的bearer标识
        return trim(str_ireplace($method, '', $header));
    }
}
PHP 常用的公共配置
<?php
// 应用公共文件
if(!function_exists('fail'))
{
    function fail($msg,$data="",$code=500)
    {
        $info = [
            'data'=>$data,
            'code'=>$code,
            'msg'=>$msg,
        ];
        return json($info);
    }
}


if(!function_exists('success'))
{
    function success($msg,$data="",$code=200)
    {
        $info = [
            'data'=>$data,
            'code'=>$code,
            'msg'=>$msg,
        ];
        return json($info);
    }
}

if(!function_exists("recursion")){
    //
    function recursion($list,$pid=0,$f=0){
        //声明一个静态数组存储处理后的数据
        static $arr = [];
        foreach($list as $val){
            if($val['pid']==$pid){
                $val['level'] = $f;
                $arr[]=$val;
                get_cate_list($list,$val['id'],$f+1);
            }
        }
        return $arr;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啥都不多头发多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值