企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 扩展库 *此处的示例从假设的依赖注入容器中获取配置对象。您可以在同一个脚本中创建它或从不同的文件中要求它。它基本上取决于您的系统是如何引导的。* -------------------------- 这个库中设计了一些扩展点。如果他们愿意,这些应该使人们能够轻松地自定义我们的核心组件。 ## 建造者 令牌构建器为普通令牌创建定义了一个流畅的接口。 要创建自己的构建器,您必须实现`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)使用它。