# 擴展庫
*此處的示例從假設的依賴注入容器中獲取配置對象。您可以在同一個腳本中創建它或從不同的文件中要求它。它基本上取決于您的系統是如何引導的。*
--------------------------
這個庫中設計了一些擴展點。如果他們愿意,這些應該使人們能夠輕松地自定義我們的核心組件。
## 建造者
令牌構建器為普通令牌創建定義了一個流暢的接口。
要創建自己的構建器,您必須實現`Lcobucci\JWT\Builder`接口:
~~~php
use Lcobucci\JWT\Builder;
final class MyCustomTokenBuilder implements Builder
{
// implement all methods
}
~~~
然后,在[配置對象中](https://lcobucci-jwt.readthedocs.io/en/latest/configuration/)注冊一個自定義工廠:
~~~php
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$config->setBuilderFactory(
static function (ClaimsFormatter $formatter): Builder {
return new MyCustomTokenBuilder($formatter);
}
);
~~~
## 索賠格式化程序
默認情況下,我們提供以下格式化程序:
* 統一受眾聲明,確保我們在聲明中只有一項時使用字符串
* 使用微秒(浮點數)格式化基于日期的聲明
您可以自定義甚至創建自己的格式化程序:
~~~php
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Configuration;
use Serializable;
final class ClaimSerializer implements ClaimsFormatter
{
/** @inheritdoc */
public function formatClaims(array $claims): array
{
foreach ($claims as $claim => $claimValue) {
if ($claimValue instanceof Serializable) {
$claims[$claim] = $claimValue->serialize();
}
}
return $claims;
}
}
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$config->builder(new ClaimSerializer());
~~~
該類`Lcobucci\JWT\Encoding\ChainedFormatter`允許用戶組合多個格式化程序。
## 解析器
令牌解析器定義了如何將 JWT 字符串轉換為令牌對象。
要創建自己的解析器,您必須實現`Lcobucci\JWT\Parser`接口:
~~~php
use Lcobucci\JWT\Parser;
final class MyCustomTokenParser implements Parser
{
// implement all methods
}
~~~
然后在[配置對象)注冊一個實例:
~~~php
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$config->setParser(new MyCustomTokenParser());
~~~
## 簽名者
簽名者定義如何創建和驗證簽名。
要創建自己的簽名者,您必須實現`Lcobucci\JWT\Signer`接口:
~~~php
use Lcobucci\JWT\Signer;
final class SignerForAVeryCustomizedAlgorithm implements Signer
{
// implement all methods
}
~~~
然后在創建置對象)的實例、發出令牌])或[驗證令牌時]傳遞它的一個實例。
## 鑰匙
密鑰對象被傳遞給簽名者并提供必要的信息來創建和驗證簽名。
要創建自己的簽名者,您必須實現`Lcobucci\JWT\Signer\Key`接口:
~~~php
use Lcobucci\JWT\Signer\Key;
final class KeyWithSomeMagicalProperties implements Key
{
// implement all methods
}
~~~
## 驗證器
令牌驗證器定義了如何將驗證約束應用于驗證或斷言令牌。
要創建自己的驗證器,您必須實現`Lcobucci\JWT\Validator`接口:
~~~php
use Lcobucci\JWT\Validator;
final class MyCustomTokenValidator implements Validator
{
// implement all methods
}
~~~
然后在[配置對象中注冊一個實例:
~~~php
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$config->setValidator(new MyCustomTokenValidator());
~~~
## 驗證約束
驗證約束定義應如何驗證一個或多個聲明/標題。自定義驗證約束可方便地為注冊聲明提供高級規則或驗證私有聲明。
要創建自己的約束實現,您必須實現`Lcobucci\JWT\Validation\Constraint`接口:
~~~php
use Lcobucci\JWT\Token;
use Lcobucci\JWT\UnencryptedToken;
use Lcobucci\JWT\Validation\Constraint;
use Lcobucci\JWT\Validation\ConstraintViolation;
final class SubjectMustBeAValidUser implements Constraint
{
public function assert(Token $token): void
{
if (! $token instanceof UnencryptedToken) {
throw new ConstraintViolation('You should pass a plain token');
}
if (! $this->existsInDatabase($token->claims()->get('sub'))) {
throw new ConstraintViolation('Token related to an unknown user');
}
}
private function existsInDatabase(string $userId): bool
{
// ...
}
}
~~~
然后在)[驗證令牌](%E9%AA%8C%E8%AF%81%E4%BB%A4%E7%89%8C.md)使用它。
- 序言
- 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定位城市
- 更新日志