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.

119 lines
4.7 KiB

# Swagger配置诊断脚本
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
Write-Host ""
Write-Host "请先启动后端服务:" -ForegroundColor Yellow
Write-Host " cd backend" -ForegroundColor White
Write-Host " go run main.go" -ForegroundColor White
exit 1
}
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.PSObject.Properties.Name)" -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"},
@{Path="/swagger-ui-fixed"; 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 UI: http://localhost:8080/swagger-ui-fixed" -ForegroundColor White
Write-Host " Swagger JSON: http://localhost:8080/swagger/doc.json" -ForegroundColor White
Write-Host ""
Write-Host "🔧 Authorize按钮不显示的可能原因:" -ForegroundColor Cyan
Write-Host " 1. Swagger UI版本问题" -ForegroundColor White
Write-Host " 2. 浏览器缓存问题" -ForegroundColor White
Write-Host " 3. 安全定义配置问题" -ForegroundColor White
Write-Host " 4. API没有使用安全配置" -ForegroundColor White
Write-Host ""
Write-Host "🎯 解决方案:" -ForegroundColor Cyan
Write-Host " 1. 使用修复版Swagger UI: http://localhost:8080/swagger-ui-fixed" -ForegroundColor White
Write-Host " 2. 清除浏览器缓存 (Ctrl+Shift+Delete)" -ForegroundColor White
Write-Host " 3. 强制刷新页面 (Ctrl+F5)" -ForegroundColor White
Write-Host " 4. 检查浏览器控制台错误信息 (F12)" -ForegroundColor White
Write-Host ""
Write-Host "📝 测试步骤:" -ForegroundColor Cyan
Write-Host " 1. 访问 http://localhost:8080/swagger-ui-fixed" -ForegroundColor White
Write-Host " 2. 检查右上角是否有Authorize按钮" -ForegroundColor White
Write-Host " 3. 如果没有,按F12查看控制台错误信息" -ForegroundColor White
Write-Host " 4. 尝试不同的浏览器" -ForegroundColor White