zoukankan      html  css  js  c++  java
  • jwt实现

    <?php

    namespace appadmincontroller;

    use thinkConfig;
    use
    thinkController;
    use
    thinkRequest;
    use
    thinkcachedriverRedis;

    /**
    * Class Base
    * @package
    appadmincontroller
    */
    class Base extends
    Controller
    {
    /**
    * Base constructor.
    * @param
    Request|null $request
    */
    public function __construct(Request $request = null
    )
    {
    not_region(true);
    //允许
    parent::__construct($request
    );
    }

    static function getAdminKey()
    {
    $admin_key = Config::get('key')["admin_key"
    ];
    return $admin_key
    ;
    }

    /**
    *
    @var
    */
    protected $head
    ;

    /**
    *
    @var
    */
    protected $payload
    ;

    /**
    * @param
    string $iss 发行者
    * @param
    string $exp 过期时间
    * @param
    $uid int 用户id
    * @return
    array
    */
    protected function payload($uid
    )
    {
    $this->payload
    = [
    'iss'
    =>request()->domain(),
    'exp'=>time
    (),
    'uid'=>$uid
    ,
    ];
    return $this->payload
    ;
    }

    protected function head()
    {
    $this->head
    = [
    'type'=>"JWT"
    ,
    'alg'=>'HA256'
    ,
    ];
    return $this->head
    ;
    }

    /**

    * @param $str
    * @return
    string
    */
    static function baseEn($str
    )
    {
    return base64_encode($str
    );
    }

    /**
    * @param
    $str
    * @return
    string
    */
    static function baseDe($str
    )
    {
    return base64_decode($str
    );
    }

    /**
    * @param
    $strdata
    * @return
    string
    */
    static function hash_hmac($strdata
    )
    {
    $hashSha256 = self::baseEn(hash_hmac("sha256",$strdata,"secret",true
    ));
    return $hashSha256
    ;
    }

    /**
    * @param
    array $head $this->head();/other
    * @param
    array $payload $this->payload();/other
    * @return
    string
    */
    static function base64_meger($head=[],$payload=[],$redis
    )
    {
    $new_arr
    = [];
    foreach (func_get_args() as $keies
    )
    {
    if(is_array($keies
    )){
    $new_arr[] = self::baseEn(json_encode($keies
    ));
    }
    }
    $new_signature = implode('.',$new_arr
    );
    // ----------- 签名生成------
    $signature = self::hash_hmac($new_signature
    );
    // ------------保存签名------
    self::setRedis($redis,$signature
    );
    $token = $new_signature.'.'.$signature
    ;

    return $token;
    }

    /**
    *
    * @param Redis
    $redis

    * @param $signature
    * @return
    bool
    */
    protected static function setRedis(
    $redis,$signature
    )
    {
    return
    $redis->set(self::getAdminKey(),$signature
    );
    }

    }

     

    实现:

    <?php
    namespace
    appadmincontroller;

    use thinkcachedriverRedis;

    /**
    * Class Index
    * @package
    appadmincontroller
    */
    class Index extends
    Base
    {
    public function index(Redis $redis
    )
    {
    $token = $this::base64_meger($this->head(),$this->payload(2),$redis
    );
    return $token
    ;
    }
    }

     

    输出:

    image


  • 相关阅读:
    《游戏引擎构架Game Engine Architecture》略读笔记
    Visual Studio快速入门(大纲)
    Linux课程学习 第四课
    Linux课程学习 第三课
    Linux课程学习 第二课
    vmware虚拟机安装centos7.3
    Win7 Linux双系统,恢复Linux启动项
    Java中Collection、Map常用实现类研究分析
    数据库计算每个表所占空间和记录数
    tomcat中部署多个项目,webAppRootKey 参数配置
  • 原文地址:https://www.cnblogs.com/q1104460935/p/6925699.html
Copyright © 2011-2022 走看看