更新依赖、添加规则
This commit is contained in:
@@ -6,13 +6,29 @@ use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* JWT 签发与解析服务
|
||||
* firebase/php-jwt 对 HS256 要求密钥至少 256 bit(32 字节),过短会抛 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
|
||||
|
||||
Reference in New Issue
Block a user