ruvents/spiral-jwt

0.1.1 2022-01-11 06:43 UTC

This package is auto-updated.

Last update: 2024-09-11 12:41:55 UTC


README

此包在 Spiral 应用中实现 JWT 令牌的授权。使用 firebase/php-jwt 进行 JWT 相关工作,参考其文档以获取支持的算法列表。

安装

composer require ruvents/spiral-jwt

然后,将 JwtAuthBootloader 添加到您的 App.php 文件中。

use Ruvents\SpiralJwt\JwtAuthBootloader;

class App extends Kernel {
    protected const LOAD = [
        ...
        JwtAuthBootloader::class,
    ]
}

配置

默认配置如下

<?php

declare(strict_types=1);

return [
    'algorithm' => 'HS256',
    'expiresAt' => '+1 week',
];

复制并放置到您的配置目录中的 jwt.php 文件中: app/config/jwt.php

您必须提供 key 用于加密和解密。

对称加密

<?php

use Ruvents\SpiralJwt\Keys;

declare(strict_types=1);

return [
    'algorithm' => 'HS256',
    'expiresAt' => '+1 week',
    'key' => new Keys('*PRIVATE KEY*'),
];

非对称加密

<?php

use Ruvents\SpiralJwt\Keys;

declare(strict_types=1);

return [
    'algorithm' => 'RS256',
    'expiresAt' => '+1 week',
    'key' => new Keys('*PRIVATE KEY*', '*PUBLIC KEY*'),
];