更新依赖、添加规则

This commit is contained in:
李琦
2026-08-10 13:58:14 +08:00
parent d718c267ef
commit 6bd74bb7d2
3 changed files with 26 additions and 2 deletions

View File

@@ -6,13 +6,29 @@ use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Exception;
/**
* JWT 签发与解析服务
* firebase/php-jwt HS256 要求密钥至少 256 bit32 字节),过短会抛 Provided key is too short
*/
class JWTService
{
private static mixed $_instance;
private string $secretKey = 'cc_spa_jwt_secret_key'; // 请替换为你的密钥
private string $secretKey;
private string $token;
/**
* 构造时从配置读取密钥,并兜底保证满足 HS256 最小长度
*/
public function __construct()
{
// 优先读环境变量 JWT_SECRET长度不足时用 hash 派生,避免库直接报错
$secret = (string) config('nl.jwt.secret', '');
if (strlen($secret) < 32) {
$secret = hash('sha256', $secret !== '' ? $secret : 'nl_admin_jwt_fallback_secret');
}
$this->secretKey = $secret;
}
/**
* 获取实例
* @return null|static