<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # :-: tp5使用lcobucci/jwt 兩種寫法的推薦第一種,使用的時3.3版本的 ,第二種寫法中有方法將會在v4版本棄用。 用的tp5.1,方法寫在extend中的 ,根據自己喜好放。第一種和第二種就是方法有點不一樣,具體看 ``` https://github.com/lcobucci/jwt/tree/3.4 ``` 安裝 ``` composer require lcobucci/jwt composer require lcobucci/jwt 3.3 // 可以指定版本 在后面加上版本號就行 ``` ___ ``` <?php namespace tools\jwt; use Lcobucci\JWT\Builder; use Lcobucci\JWT\Parser; use Lcobucci\JWT\Signer\Hmac\Sha256; use Lcobucci\JWT\Signer\Key; use Lcobucci\JWT\ValidationData; class Token { private static $_config = [ 'audience' => 'http://www.test.cn',//接收人 'id' => 'qwertyuio',//token的唯一標識,這里只是一個簡單示例 'issuer' => 'http://api.test.cn',//簽發人 'expire' => 3600*24, //有效期 'secret' =>'thisisasecret', // 秘鑰 ]; //生成token public static function getToken($user_id){ //簽名對象 $signer = new Sha256(); //獲取當前時間戳 $time = time(); //設置簽發人、接收人、唯一標識、簽發時間、立即生效、過期時間、用戶id、簽名 $token = (new Builder())->issuedBy(self::$_config['issuer']) ->permittedFor(self::$_config['audience']) ->identifiedBy(self::$_config['id'], true) ->issuedAt($time) ->canOnlyBeUsedAfter($time-1) ->expiresAt($time + self::$_config['expire']) ->withClaim('user_id', $user_id) ->getToken($signer,new Key( self::$_config['secret'])); return (string)$token; } //從請求信息中獲取token令牌 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)); } //從token中獲取用戶id (包含token的校驗) public static function getUserId($token = null) { $user_id = null; $token = empty($token)?self::getRequestToken():$token; if (!empty($token)) { //為了注銷token 加以下if判斷代碼 $delete_token = cache('delete_token') ?: []; if(in_array($token, $delete_token)){ //token已被刪除(注銷) return $user_id; } $token = (new Parser())->parse((string) $token); //驗證token $data = new ValidationData(); $data->setIssuer(self::$_config['issuer']);//驗證的簽發人 $data->setAudience(self::$_config['audience']);//驗證的接收人 $data->setId(self::$_config['id']);//驗證token標識 if (!$token->validate($data)) { //token驗證失敗 return $user_id; } //驗證簽名 $signer = new Sha256(); if (!$token->verify($signer, self::$_config['secret'])) { //簽名驗證失敗 return $user_id; } //從token中獲取用戶id $user_id = $token->getClaim('user_id'); } return $user_id; } } ``` 第二種 ___ ``` <?php namespace tools\jwt; use Lcobucci\JWT\Builder; use Lcobucci\JWT\Parser; use Lcobucci\JWT\Signer\Hmac\Sha256; use Lcobucci\JWT\ValidationData; class Token { private static $_config = [ 'audience' => 'http://www.test.cn',//接收人 'id' => 'qwertyuio',//token的唯一標識,這里只是一個簡單示例 'sign' => 'thisisasign',//簽名密鑰 'issuer' => 'http://api.test.cn',//簽發人 'expire' => 3600*24 //有效期 ]; //生成token public static function getToken($user_id){ //簽名對象 $signer = new Sha256(); //獲取當前時間戳 $time = time(); //設置簽發人、接收人、唯一標識、簽發時間、立即生效、過期時間、用戶id、簽名 $token = (new Builder())->issuedBy(self::$_config['issuer']) ->canOnlyBeUsedBy(self::$_config['audience']) ->identifiedBy(self::$_config['id'], true) ->issuedAt($time) ->canOnlyBeUsedAfter($time-1) ->expiresAt($time + self::$_config['expire']) ->with('user_id', $user_id) ->sign($signer, self::$_config['sign']) ->getToken(); return (string)$token; } //從請求信息中獲取token令牌 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)); } //從token中獲取用戶id (包含token的校驗) public static function getUserId($token = null) { $user_id = null; $token = empty($token)?self::getRequestToken():$token; if (!empty($token)) { //為了注銷token 加以下if判斷代碼 $delete_token = cache('delete_token') ?: []; if(in_array($token, $delete_token)){ //token已被刪除(注銷) return $user_id; } $token = (new Parser())->parse((string) $token); //驗證token $data = new ValidationData(); $data->setIssuer(self::$_config['issuer']);//驗證的簽發人 $data->setAudience(self::$_config['audience']);//驗證的接收人 $data->setId(self::$_config['id']);//驗證token標識 if (!$token->validate($data)) { //token驗證失敗 return $user_id; } //驗證簽名 $signer = new Sha256(); if (!$token->verify($signer, self::$_config['sign'])) { //簽名驗證失敗 return $user_id; } //從token中獲取用戶id $user_id = $token->getClaim('user_id'); } return $user_id; } } ``` ## 基類實例寫法 ___ ``` <?php namespace app\api\controller; use think\Controller; use think\Facade\Request; use tools\jwt\Token; class Base extends Controller { protected $uid; protected static $noLogin=['index/test','index/login']; //不需要登錄的接口 public function initialize() { parent::initialize(); // TODO: Change the autogenerated stub header("Access-Control-Allow-Origin:*"); header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE"); header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding"); try{ $url=strtolower(Request::controller()).'/'.Request::action(); if(!in_array($url,self::$noLogin)){ $user_id=Token::getUserId(); if(empty($user_id)){ echo json(['msg'=>'token不能為空','code'=>403])->send();die; } } // 可以把uid放請求里面 還可以丟屬性 $this->uid=$user_id); $this->request->get('user_id',$user_id); $this->request->post('user_id',$user_id); }catch (\Exception $e){ echo json(['msg'=>'token驗證失敗','code'=>403])->send();die; } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看