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


  • 相关阅读:
    从Dojo1.6到Dojo1.8(二)—— 基本模块,事件,约定,DOM操作
    从Dojo1.6到Dojo1.8(三)—— Parser,Dijit,DojoX
    在Node.js上使用dojo库进行面向对象web应用开发
    JavaScript事件代理和委托(Delegation)
    介绍一款神级API接口神器,高效工作,告别加班!
    sqlserver备份存储过程
    WSGI原理与简单实现
    从Python看Web架构的发展
    Celery:小试牛刀
    Flask:用户角色与权限管理
  • 原文地址:https://www.cnblogs.com/q1104460935/p/6925699.html
Copyright © 2011-2022 走看看