aleksandarjockovic226/php-jwt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aleksandarjockovic226/php-jwt

Composer 安装命令:

composer require aleksandarjockovic226/php-jwt

包简介

Framework agnostic JWT library for PHP.

README 文档

README

Simple, framework-agnostic JWT library for PHP with RS256 support.

Features

  • JWT encoding and decoding
  • RS256 signing and verification
  • Custom key providers
  • Token validation
  • Expiration (exp) validation
  • Issued At (iat) validation
  • Not Before (nbf) validation
  • Framework agnostic
  • Fully typed (strict_types=1)
  • PHPUnit tested
  • PHPStan clean

Installation

composer require aleksandarjockovic226/php-jwt

Generate RSA Keys

Generate a private key:

openssl genrsa -out private.pem 2048

Generate the corresponding public key:

openssl rsa -in private.pem -pubout -out public.pem

Basic Usage

Create JWT Service

use aleksandarjockovic226\Jwt\Algorithms\RsaSha256Algorithm;
use aleksandarjockovic226\Jwt\Factories\DefaultJwtHeaderFactory;
use aleksandarjockovic226\Jwt\Providers\FileKeyProvider;
use aleksandarjockovic226\Jwt\Services\JwtService;
use aleksandarjockovic226\Jwt\Support\TokenParser;

$jwt = new JwtService(
    algorithm: new RsaSha256Algorithm(
        new FileKeyProvider(
            privateKeyPath: __DIR__ . '/private.pem',
            publicKeyPath: __DIR__ . '/public.pem',
        ),
    ),
    headerFactory: new DefaultJwtHeaderFactory(),
    tokenParser: new TokenParser(),
);

Encode Token

use aleksandarjockovic226\Jwt\Builders\JwtPayloadBuilder;

$payload = JwtPayloadBuilder::create()
    ->claim('sub', 123)
    ->claim('email', 'john@example.com')
    ->claim('role', 'admin')
    ->build();

$token = $jwt->encode($payload);

echo $token;

You can also set multiple claims at once with claims():

$payload = JwtPayloadBuilder::create()
    ->claims([
        'sub' => 123,
        'email' => 'john@example.com',
        'role' => 'admin',
    ])
    ->build();

Decode Token

$payload = $jwt->decode($token);

echo $payload->get('sub');
echo $payload->get('email');

Verify Token

$isValid = $jwt->verify($token);

if ($isValid) {
    echo 'Token is valid';
}

Using Expiration Validation

use aleksandarjockovic226\Jwt\Support\SystemClock;
use aleksandarjockovic226\Jwt\Validators\ExpirationValidator;

$jwt = new JwtService(
    algorithm: $algorithm,
    headerFactory: new DefaultJwtHeaderFactory(),
    tokenParser: new TokenParser(),
    validators: [
        new ExpirationValidator(
            new SystemClock(),
        ),
    ],
);

Generate token:

$payload = JwtPayloadBuilder::create()
    ->claim('sub', 123)
    ->claim('exp', time() + 3600)
    ->build();

Using Issued At Validation

use aleksandarjockovic226\Jwt\Validators\IssuedAtValidator;

$jwt = new JwtService(
    algorithm: $algorithm,
    headerFactory: new DefaultJwtHeaderFactory(),
    tokenParser: new TokenParser(),
    validators: [
        new IssuedAtValidator(
            new SystemClock(),
        ),
    ],
);
$payload = JwtPayloadBuilder::create()
    ->claim('iat', time())
    ->build();

Using Not Before Validation

use aleksandarjockovic226\Jwt\Validators\NotBeforeValidator;

$jwt = new JwtService(
    algorithm: $algorithm,
    headerFactory: new DefaultJwtHeaderFactory(),
    tokenParser: new TokenParser(),
    validators: [
        new NotBeforeValidator(
            new SystemClock(),
        ),
    ],
);
$payload = JwtPayloadBuilder::create()
    ->claim('nbf', time() + 60)
    ->build();

The token will become valid after 60 seconds.

Combining Validators

$jwt = new JwtService(
    algorithm: $algorithm,
    headerFactory: new DefaultJwtHeaderFactory(),
    tokenParser: new TokenParser(),
    validators: [
        new ExpirationValidator(
            new SystemClock(),
        ),
        new IssuedAtValidator(
            new SystemClock(),
        ),
        new NotBeforeValidator(
            new SystemClock(),
        ),
    ],
);

In-Memory Keys

Useful when keys are stored in environment variables or secrets managers.

use aleksandarjockovic226\Jwt\Providers\InMemoryKeyProvider;

$keyProvider = new InMemoryKeyProvider(
    privateKey: getenv('JWT_PRIVATE_KEY'),
    publicKey: getenv('JWT_PUBLIC_KEY'),
);

Access Payload Claims

$payload->has('email');

$payload->get('email');

$payload->get('missing', 'default-value');

$payload->all();

Exceptions

The package throws specialized exceptions:

JwtException
├── InvalidTokenException
├── InvalidHeaderException
├── InvalidPayloadException
├── InvalidSignatureException
├── TokenExpiredException
├── InvalidClaimException
├── KeyException
└── AlgorithmException

Example:

try {
    $payload = $jwt->decode($token);
} catch (TokenExpiredException $e) {
    echo 'Token expired';
} catch (InvalidSignatureException $e) {
    echo 'Invalid signature';
}

Requirements

  • PHP 8.5+
  • OpenSSL extension

License

MIT License

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固