您当前的位置: 首页 >  ar

知其黑、受其白

暂无认证

  • 0浏览

    0关注

    1250博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

laravel firebase/php-jwt token验证

知其黑、受其白 发布时间:2021-08-18 17:37:47 ,浏览量:0

laravel firebase/php-jwt token验证
  • 1、说明
  • 2、安装
  • 3、使用
    • 3.1、签发token
    • 3.2、验证token
  • 4、综合运用
    • 4.1、使用场景
    • 4.2、使用
      • ①、安装
      • ②、封装2个方法
      • ③、定义1个中间件
      • ④、app/Http/Kernel.php 应用的路由中间件列表
      • ⑤、定义路由
      • ⑥、控制器中
      • ⑦、结果

1、说明

这里的jwt和之前另外一篇的jwt有些不一样,之前的是基于用户的接口验证,即需要接口登录,此jwt则是基于一个唯一标识,如移动设备唯一ID或手机号码等能够作为唯一标识的数据信息,通过它来完成token签发、验证。

2、安装
composer require firebase/php-jwt
3、使用 3.1、签发token
public function createToken()
{
    $key = '344'; //key,唯一标识
    $time = time(); //当前时间
    $token = [
        'iat' => $time, //签发时间
        'nbf' => $time , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用
        'exp' => $time+7200, //过期时间,这里设置2个小时
        'data' => [ //自定义信息,不要定义敏感信息
            'device_id' => 'asdfghj',
        ]
    ];
    $token = JWT::encode($token, $key,'HS256'); //签发token
    $data = ['error'=>0,'mgs'=>'success','data'=>['token'=>$token]];
    return json_encode($data);
}

3.2、验证token
// $token:签发的token
public function verifyToken($token)
{
    $key = '344'; //key要和签发的时候一样,唯一标识
    try {
        JWT::$leeway = 60;//当前时间减去60,把时间留点余地
        $decoded = JWT::decode($token, $key, ['HS256']); //HS256方式,这里要和签发的时候对应
        $arr = (array)$decoded;
        print_r($arr);
    } catch(\Firebase\JWT\SignatureInvalidException $e) {  //签名不正确
        echo $e->getMessage();
    }catch(\Firebase\JWT\BeforeValidException $e) {  // 签名在某个时间点之后才能用
        echo $e->getMessage();
    }catch(\Firebase\JWT\ExpiredException $e) {  // token过期
        echo $e->getMessage();
    }catch(Exception $e) {  //其他错误
        echo $e->getMessage();
    }
}

4、综合运用 4.1、使用场景

项目中app接口需要接口验证,又不要登录,app端能够提供1个唯一标识设备唯一ID,通过这个ID生成token,并通过这个ID和token来完成接口验证。

4.2、使用 ①、安装
composer require firebase/php-jwt
②、封装2个方法

1个用来生成token,1个用来刷新token

key:设备唯一ID的加密字符串,接口请求时也需要提供,验证的时候需要用到。

# 生成并返回token
public function getToken($param)
{
    $deviceId = isset($param['device_id']) ? $param['device_id'] : '';
    //device_id做为唯一标识
    
    if(empty($deviceId)){
        return $this->responseInvalidParams('device_id is empty');
    }
    $key = encrypt($deviceId); //key
    $time = time(); //当前时间
    $overtime = 7200;
    $token = [
        'iat' => $time, //签发时间
        'nbf' => $time , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用
        'exp' => $time+$overtime, //过期时间,这里设置2个小时
    ];
    if(!$token = JWT::encode($token, $key,'HS256')){
        \Log::error('app api operation failed:key='.$key.', token='.$token);
        return  response()->json(['error'=>1,'msg'=>'operation failed','data'=>[]]);
    }
    return  response()->json(['error'=>0,'msg'=>'ok','data'=>['key'=>$key,'token'=>$token,'expire'=>$overtime]]);
}

# 重新生成并返回token
public function refreshToken($key)
{
    if(empty($key)){
        return $this->responseInvalidParams('key is empty');
    }

    $newKey = encrypt(decrypt($key));//重新生成key
    $time = time(); //当前时间
    $overtime = 7200;
    $token = [
        'iat' => $time, //签发时间
        'nbf' => $time , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用
        'exp' => $time+$overtime, //过期时间,这里设置2个小时
    ];
    if(!$token = JWT::encode($token, $newKey,'HS256')){
        \Log::error('app api operation failed:key='.$newKey.', token='.$token);
        return  response()->json(['error'=>1,'msg'=>'operation failed','data'=>[]]);
    }

    return  response()->json(['error'=>0,'msg'=>'ok','data'=>['key'=>$key,'token'=>$token,'expire'=>$overtime]]);
}

③、定义1个中间件

通过中间件,让接口每次请求是都去验证token

php artisan make:middleware VerifySign //生成中间件

VerifySign:验证token

            
关注
打赏
1665558895
查看更多评论
0.0534s