feat(api): 初始化笔记应用后端基础架构
- 添加环境配置文件 .env.example 包含数据库、JWT、CORS等配置 - 创建 .gitignore 文件忽略敏感文件和临时文件 - 配置 Apache 重写规则支持路由转发 - 实现 JWT 认证中间件提供用户身份验证功能 - 添加 MySQL 数据库初始化脚本包含分组、图片、笔记表结构
This commit is contained in:
45
biji-php/src/Utils/JWTUtil.php
Normal file
45
biji-php/src/Utils/JWTUtil.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
class JWTUtil
|
||||
{
|
||||
private static $secret;
|
||||
private static $expire;
|
||||
|
||||
public static function init()
|
||||
{
|
||||
self::$secret = $_ENV['JWT_SECRET'] ?? 'default-secret-key';
|
||||
self::$expire = (int)($_ENV['JWT_EXPIRE'] ?? 86400);
|
||||
}
|
||||
|
||||
public static function encode($userId, $username)
|
||||
{
|
||||
self::init();
|
||||
|
||||
$payload = [
|
||||
'iss' => 'biji-php',
|
||||
'iat' => time(),
|
||||
'exp' => time() + self::$expire,
|
||||
'userId' => $userId,
|
||||
'username' => $username
|
||||
];
|
||||
|
||||
return JWT::encode($payload, self::$secret, 'HS256');
|
||||
}
|
||||
|
||||
public static function decode($token)
|
||||
{
|
||||
self::init();
|
||||
|
||||
try {
|
||||
$decoded = JWT::decode($token, new Key(self::$secret, 'HS256'));
|
||||
return (array)$decoded;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user