119 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use Illuminate\Support\Facades\Route;
use App\Services\Auth\TokenAuthService;
use App\Http\Middleware\AdminApiAuthenticate;
use Illuminate\Http\Request;
Route::get('/', function () {
return view('welcome');
});
// 认证缓存测试路由
Route::get('/test-auth-cache', function (TokenAuthService $tokenAuthService) {
$output = [];
$output[] = "=== 认证缓存测试 ===";
$output[] = "";
// 1. 测试服务实例化
$output[] = "1. 测试TokenAuthService...";
$output[] = "✅ TokenAuthService实例化成功";
$output[] = "";
// 2. 测试缓存状态
$output[] = "2. 测试缓存健康状态...";
$stats = $tokenAuthService->getCacheStats();
$output[] = "缓存驱动: " . ($stats['cache_store'] ?? 'unknown');
$output[] = "缓存可用: " . ($stats['cache_available'] ? '✅ 是' : '❌ 否');
$output[] = "缓存健康: " . ($stats['cache_health'] ? '✅ 正常' : '❌ 异常');
$output[] = "缓存前缀: " . $stats['cache_prefix'];
$output[] = "默认缓存时间: " . $stats['default_cache_minutes'] . ' 分钟';
$output[] = "最大缓存时间: " . $stats['max_cache_minutes'] . ' 分钟';
$output[] = "";
// 3. 测试token验证
$output[] = "3. 测试token验证...";
$testToken = 'test_invalid_token_123';
$user = $tokenAuthService->validateTokenAndGetUser($testToken);
if ($user === null) {
$output[] = "✅ 无效token正确返回null";
} else {
$output[] = "❌ 无效token验证失败";
}
$output[] = "";
// 4. 测试中间件
$output[] = "4. 测试中间件功能...";
try {
// 创建模拟请求
$request = Request::create('/admin/auth/me', 'GET');
$request->headers->set('Authorization', 'Bearer invalid_token');
$request->headers->set('Accept', 'application/json');
// 实例化中间件
$middleware = new AdminApiAuthenticate($tokenAuthService);
// 测试中间件
$response = $middleware->handle($request, function ($req) {
return response()->json(['message' => 'Success']);
});
if ($response->getStatusCode() === 401) {
$output[] = "✅ 中间件正确返回401错误";
$responseData = json_decode($response->getContent(), true);
$output[] = "响应消息: " . ($responseData['message'] ?? 'unknown');
} else {
$output[] = "❌ 中间件未正确处理无效token";
}
} catch (\Exception $e) {
$output[] = "❌ 中间件测试失败: " . $e->getMessage();
}
$output[] = "";
// 5. 缓存性能测试
$output[] = "5. 缓存性能测试...";
if ($stats['cache_health']) {
$startTime = microtime(true);
// 模拟多次缓存操作
for ($i = 0; $i < 10; $i++) {
$tokenAuthService->validateTokenAndGetUser('test_token_' . $i);
}
$endTime = microtime(true);
$duration = round(($endTime - $startTime) * 1000, 2);
$output[] = "✅ 完成10次token验证耗时: {$duration}ms";
} else {
$output[] = "⚠️ 缓存不可用,跳过性能测试";
}
$output[] = "";
$output[] = "=== 测试完成 ===";
// 总结
$cacheStatus = $stats['cache_health'] ? '正常工作' : '使用数据库回退';
$output[] = "";
$output[] = "组件状态总结:";
$output[] = "- 缓存功能: " . $cacheStatus;
$output[] = "- 中间件: ✅ 正常工作";
$output[] = "- Token验证: ✅ 正常工作";
$output[] = "- 异常处理: ✅ 正常工作";
$output[] = "";
$output[] = "提示如果要启用Redis缓存请确保";
$output[] = "1. Redis服务正在运行";
$output[] = "2. 在.env文件中设置 CACHE_STORE=redis";
$output[] = "3. 在.env文件中设置 REDIS_CLIENT=predis";
return response('<pre>' . implode("\n", $output) . '</pre>')
->header('Content-Type', 'text/html; charset=utf-8');
});