要發布新令牌,您必須創建一個新令牌(使用[配置](%E9%85%8D%E7%BD%AE.md)時更容易),自定義它,并要求它構建令牌:
~~~php
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$now = new DateTimeImmutable();
$token = $config->builder()
// 配置頒發者(iss聲明)
->issuedBy('http://example.com')
// 配置訪問群體(aud聲明)
->permittedFor('http://example.org')
// 配置id(jti聲明)
->identifiedBy('4f1g23a12aa')
// 配置令牌發出的時間(iat聲明)
->issuedAt($now)
// 配置可使用令牌的時間(nbf聲明)
->canOnlyBeUsedAfter($now->modify('+1 minute'))
// 配置令牌的過期時間(exp聲明)
->expiresAt($now->modify('+1 hour'))
// 配置名為“username”的新聲明
->withClaim('username', "admin")
// 配置名為“foo”的新請求頭
->withHeader('foo', 'bar')
// 構建一個新令牌
->getToken($config->signer(), $config->signingKey());
~~~
創建令牌后,您就可以檢索其數據并將其轉換為字符串表示形式:
~~~php
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$token = $config->builder()
->issuedBy('http://example.com')
->withClaim('uid', 1)
->withHeader('foo', 'bar')
->getToken($config->signer(), $config->signingKey());
$token->headers(); // /檢索 headers
$token->claims(); // /檢索聲明
echo $token->headers()->get('foo'); // will print "bar"
echo $token->claims()->get('iss'); // will print "http://example.com"
echo $token->claims()->get('uid'); // will print "1"
echo $token->toString(); // 字符串表示形式JWT字符串
~~~
# 實例
~~~
<?php
require 'vendor/autoload.php';
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
$config = Configuration::forSymmetricSigner(
//您可以使用任何HMAC變體(256、384和512)
new Sha256(),
// 用您自己的密鑰替換下面的值!
InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw=')
// 您還可以通過在此處提供額外參數來覆蓋JOSE編碼器/解碼器
);
assert($config instanceof Configuration);
$now = new DateTimeImmutable();
$token = $config->builder()
// 配置頒發者(iss聲明)
->issuedBy('http://example.com')
// 配置訪問群體(aud聲明)
->permittedFor('http://example.org')
// 配置id(jti聲明)
->identifiedBy('4f1g23a12aa')
// 配置令牌發出的時間(iat聲明)
->issuedAt($now)
// 配置可使用令牌的時間(nbf聲明)
->canOnlyBeUsedAfter($now->modify('+1 minute'))
// 配置令牌的過期時間(exp聲明)
->expiresAt($now->modify('+1 hour'))
// 配置名為“username”的新聲明
->withClaim('username', "admin")
// 配置名為“foo”的新請求頭
->withHeader('foo', 'bar')
// 構建一個新令牌
->getToken($config->signer(), $config->signingKey());
var_dump($token->toString());
// string(319) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImZvbyI6ImJhciJ9.eyJpc3MiOiJodHRwOi8vZXhhbXBsZS5jb20iLCJhdWQiOiJodHRwOi8vZXhhbXBsZS5vcmciLCJqdGkiOiI0ZjFnMjNhMTJhYSIsImlhdCI6MTYyODczNTUxOC4wMjczODYsIm5iZiI6MTYyODczNTU3OC4wMjczODYsImV4cCI6MTYyODczOTExOC4wMjczODYsInVzZXJuYW1lIjoiYWRtaW4ifQ.JiRuboBISaDDEW7d3TPpeZ-pjeJTJM3AcScfrcFX\_do"
~~~
- 序言
- ThinkPHP官方資源
- 術語
- 根目錄
- php術語
- jwt
- 下載jwt
- 認識jwt
- 生成token
- 驗證token
- lcobucci/jwt
- 安裝
- 配置
- 生成token
- 解析令牌
- 驗證令牌
- 擴展庫jwt
- thinkPHP使用lcobucci/jwt
- phpmailer
- PHPMailer的使用
- phpMailer config
- 短信驗證嗎
- 阿里云短信驗證碼發送類
- 權限管理
- 基于thinkphp6.0
- 通用函數
- 密碼加密
- 數組
- 數據庫
- 查詢數據
- 添加數據
- 刪除數據
- 批量刪除
- 更新數據
- 請求流程
- thinkphp6安裝
- thinkphp6目錄介紹
- 單應用
- 多應用
- 配置文件
- 模型,模板與Model的區別
- .env介紹
- 入口文件
- 控制器
- model層
- 視圖層
- common公共函數
- 路由
- 命令行
- 常用thinkphp函數和方法
- 高德地圖i定位城市
- 更新日志