搜尋
首頁php框架ThinkPHPThinkPHP5 使用 JWT 進行加密

ThinkPHP5 使用 JWT 進行加密

Aug 19, 2019 pm 02:18 PM
thinkphp5

ThinkPHP5 使用 JWT 進行加密

使用Github 的firebase\JWT

#- 使用Composer 安裝此擴充功能

##- 程式碼範例

<?php
/**
 * [InterCommon-接口公用]
 * @Author   RainCyan
 * @DateTime 2019-08-12T16:38:08+0800
 */
namespace app\hladmin\controller;
use think\Controller;
use \Firebase\JWT\JWT;
class InterCommonController extends Controller {
    private $key = "123456789";
    //客户端获取TOKEN
    public function _getJwtToken(){
        try {
            $string = input("string");
            if (empty($string)) {
                throw new \Exception("请传入需要加密string", -105);
            }
            $jwt = $this->_setJwtToken($string);
            throw new \Exception($jwt, 200);
        } catch (\Exception $e) {
            return json(array("code"=>$e->getCode(), "msg"=>$e->getMessage()));
        }
    }
    //签发token
    private function _setJwtToken($string=""){
        $key = $this->key;
        $time = time();
        $token = array(
            "iss" => "http://ml.cn",
            "aud" => "http://ml.cn",
            &#39;iat&#39; => $time, //签发时间
            &#39;nbf&#39; => $time + 10, //在什么时间之后该jwt才可用
            &#39;exp&#39; => $time + 10, //过期时间
            "string" => $string
        );
        $jwt = JWT::encode($token, $key);
        return $jwt;
    }
    //解析token
    protected function _readJwtToken($jwt){
        $key = $this->key;
        try {
            JWT::$leeway = 60;//当前时间减去60,把时间留点余地
            $decoded = JWT::decode($jwt, $key, [&#39;HS256&#39;]); //HS256方式,这里要和签发的时候对应
            $arr = (array)$decoded;
            return json_msg(200, "success", $arr);
        } catch(\Firebase\JWT\SignatureInvalidException $e) {  //签名不正确
            return json_msg(-101, $e->getMessage());
        }catch(\Firebase\JWT\BeforeValidException $e) {  // 签名在某个时间点之后才能用
            return json_msg(-102, $e->getMessage());
        }catch(\Firebase\JWT\ExpiredException $e) {  // token过期
            return json_msg(-103, $e->getMessage());
        }catch(Exception $e) {  //其他错误
            return json_msg(-104, $e->getMessage());
        }
    }
    //测试解析
    public function _writeJwtToken($token){
        halt($this->_readJwtToken($token));
    }
}
?>

本文來自ThinkPHP框架技術文章欄:

http://www.php.cn/phpkj/thinkphp/#

以上是ThinkPHP5 使用 JWT 進行加密的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:learnku。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。