You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

128 lines
5.0 KiB

# Swagger身份验证配置测试脚本
param(
[switch]$StartBackend
)
Write-Host "🔍 Swagger身份验证配置测试" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""
# 检查后端服务状态
Write-Host "📋 检查后端服务状态..." -ForegroundColor Yellow
try {
$response = Invoke-WebRequest -Uri "http://localhost:8080/health" -TimeoutSec 5 -ErrorAction Stop
Write-Host "✅ 后端服务正在运行 (状态码: $($response.StatusCode))" -ForegroundColor Green
} catch {
Write-Host "❌ 后端服务未运行或无法访问" -ForegroundColor Red
Write-Host " 错误信息: $($_.Exception.Message)" -ForegroundColor Red
if ($StartBackend) {
Write-Host ""
Write-Host "🚀 正在启动后端服务..." -ForegroundColor Yellow
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd backend; go run main.go"
Write-Host "⏳ 等待后端服务启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 10
try {
$response = Invoke-WebRequest -Uri "http://localhost:8080/health" -TimeoutSec 5 -ErrorAction Stop
Write-Host "✅ 后端服务启动成功" -ForegroundColor Green
} catch {
Write-Host "❌ 后端服务启动失败" -ForegroundColor Red
return
}
} else {
return
}
}
Write-Host ""
# 测试Swagger JSON文档
Write-Host "🔍 测试Swagger JSON文档..." -ForegroundColor Yellow
try {
$response = Invoke-WebRequest -Uri "http://localhost:8080/swagger/doc.json" -TimeoutSec 5 -ErrorAction Stop
$swaggerDoc = $response.Content | ConvertFrom-Json
Write-Host "✅ Swagger JSON文档获取成功" -ForegroundColor Green
# 检查安全定义
if ($swaggerDoc.securityDefinitions) {
Write-Host "✅ 安全定义配置存在" -ForegroundColor Green
Write-Host " 安全定义: $($swaggerDoc.securityDefinitions | ConvertTo-Json -Depth 1)" -ForegroundColor White
} else {
Write-Host "❌ 安全定义配置缺失" -ForegroundColor Red
}
# 检查API安全配置
$secureApis = @()
foreach ($path in $swaggerDoc.paths.PSObject.Properties) {
foreach ($method in $path.Value.PSObject.Properties) {
if ($method.Value.security) {
$secureApis += "$($path.Name) [$($method.Name)]"
}
}
}
if ($secureApis.Count -gt 0) {
Write-Host "✅ 发现 $($secureApis.Count) 个需要认证的API" -ForegroundColor Green
foreach ($api in $secureApis) {
Write-Host " - $api" -ForegroundColor White
}
} else {
Write-Host "❌ 没有发现需要认证的API" -ForegroundColor Red
}
} catch {
Write-Host "❌ 获取Swagger JSON文档失败: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host ""
# 测试Swagger UI页面
Write-Host "🔍 测试Swagger UI页面..." -ForegroundColor Yellow
$swaggerUIs = @(
@{Path="/swagger/index.html"; Name="默认Swagger UI"},
@{Path="/swagger-ui"; Name="自定义Swagger UI"}
)
foreach ($ui in $swaggerUIs) {
try {
$uri = "http://localhost:8080$($ui.Path)"
$response = Invoke-WebRequest -Uri $uri -TimeoutSec 5 -ErrorAction Stop
Write-Host "$($ui.Name): 页面加载成功" -ForegroundColor Green
} catch {
$errorMsg = if ($_.Exception.Response) {
"HTTP $($_.Exception.Response.StatusCode)"
} else {
$_.Exception.Message
}
Write-Host "$($ui.Name): $errorMsg" -ForegroundColor Red
}
}
Write-Host ""
# 显示访问信息
Write-Host "💡 访问信息:" -ForegroundColor Cyan
Write-Host " 默认Swagger UI: http://localhost:8080/swagger/index.html" -ForegroundColor White
Write-Host " 自定义Swagger UI: http://localhost:8080/swagger-ui" -ForegroundColor White
Write-Host " Swagger JSON: http://localhost:8080/swagger/doc.json" -ForegroundColor White
Write-Host ""
Write-Host "🔧 身份验证说明:" -ForegroundColor Cyan
Write-Host " 1. 如果Authorize按钮不显示,请尝试以下方法:" -ForegroundColor White
Write-Host " - 使用自定义Swagger UI: http://localhost:8080/swagger-ui" -ForegroundColor White
Write-Host " - 检查浏览器控制台是否有错误信息" -ForegroundColor White
Write-Host " - 清除浏览器缓存并重新加载" -ForegroundColor White
Write-Host " 2. 确保API有 @Security BearerAuth 注释" -ForegroundColor White
Write-Host " 3. 确保main.go中有 @securityDefinitions.apikey 配置" -ForegroundColor White
Write-Host ""
Write-Host "🎯 故障排除步骤:" -ForegroundColor Cyan
Write-Host " 1. 检查Swagger JSON文档是否包含securityDefinitions" -ForegroundColor White
Write-Host " 2. 检查API是否包含security配置" -ForegroundColor White
Write-Host " 3. 尝试不同的Swagger UI版本" -ForegroundColor White
Write-Host " 4. 检查浏览器开发者工具中的网络请求" -ForegroundColor White