php jwt的运用,PHP中对JWT的使用【原创】

使用token进行身份验证过程

1.客户端使用账号密码进行登录

2.服务端接受到请求后验证账号以及密码的正确性,若正确则服务端回传一个Token

3.客户端接收到Token后对其进行存储,每次访问时需携带token

4.服务端在接受到客户端请求时需验证token,验证成功则回传数据。

生成与验证token的方法有很多种,这里使用的是jwt( Json Web Token)。

composer.json中引入firebase/php-jwt,composer后就可以使用了。

8570a3d9e13ce4dd8f6eb87b7bd85617.png

class JWTTool extends Controller {

public function __construct(ContainerInterface $container = null)

{

header("Content-Type: text/html; charset=utf-8");

$this->setContainer($container);

}

private $key = 'yayuanzi';//密钥

private $iss = "http://example.org/send";//签发者

private $aud = "http://example.org/accept";//接受者

/**

* @param $data 加密的数据

* @param int $is_exp 是否加入有效时间

* @param int $time 有效时长

* @return string

*/

public function generateToken($data,$is_exp = 1,$time = 86400){

$token['iss'] = $this->iss;

$token['aud'] = $this->aud;

$token['iat'] = strtotime(date('Y-m-d H:i:s'));

$token['aud'] = strtotime(date('Y-m-d H:i:s'));

if($is_exp){

$token['exp'] = strtotime(date('Y-m-d H:i:s'))+$time;

}

$token['data'] = $data;

$jwt = JWT::encode($token, $this->key);

return $jwt;

}

/**

*

* @param $jwt

* @param $client 平台号

* @return array|\Symfony\Component\HttpFoundation\Response

*/

public function verificationToken($jwt,$client)

{

$key = $this->key; //key要和签发的时候一样

try {

JWT::$timestamp = strtotime(date('Y-m-d H:i:s'));//当前时间

$decoded = JWT::decode($jwt, $key, ['HS256']); //HS256方式,这里要和签发的时候对应

if(empty($decoded->data)){

throw new Exception('未登录');

}

if(empty($decoded->data->client)){

throw new Exception('非法操作,端口错误');

}

if($decoded->data->client != $client){

throw new Exception('非法操作,端口错误');

}

return Responses::arrays(

'登录成功',

0,

['user_id'=>$decoded->data->user_id]

);

} catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确

return Responses::arrays('签名错误',1);

}catch(\Firebase\JWT\BeforeValidException $e) { //

return Responses::arrays($e->getMessage(),1);

}catch(\Firebase\JWT\ExpiredException $e) { // token过期

return Responses::arrays('登录凭证失效',-1);

}catch(Exception $e) { //其他错误

return Responses::arrays($e->getMessage());

}

}

public function verificationOther($jwt,$data)

{

$key = $this->key; //key要和签发的时候一样

try {

JWT::$timestamp = strtotime(date('Y-m-d H:i:s'));//当前时间

$decoded = JWT::decode($jwt, $key, ['HS256']); //HS256方式,这里要和签发的时候对应

$tag_data = (array)$decoded->data;

foreach ($data as $k=>$v){

if(!array_key_exists($k,$tag_data)){

throw new Exception('验证失败');

}

if($tag_data[$k] != $data[$k]){

throw new Exception('验证失败');

}

}

return Responses::arrays(

'验证成功',

0,

$data

);

} catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确

return Responses::arrays('签名错误');

}catch(\Firebase\JWT\BeforeValidException $e) { //

return Responses::arrays($e->getMessage());

}catch(\Firebase\JWT\ExpiredException $e) { // token过期

return Responses::arrays('凭证失效',1);

}catch(Exception $e) { //其他错误

return Responses::arrays($e->getMessage());

}

}

}

public function testAction(){

$JWTTool = new JWTTool($this->container);

$token = $JWTTool->generateToken(['client'=>'user','user_id'=>1],0);

$res = $JWTTool->verificationOther($token,['client'=>'user','user_id'=>1]);

return new JsonResponse($res);

}

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/24496.html

75d087ef9a9fb11dc373caaf33adbf7f.png

微信打赏

支付宝打赏

感谢您对作者Alex的打赏,我们会更加努力!    如果您想成为作者,请点我

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值