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.
 
 
 
 
 
 

154 lines
6.2 KiB

# GoFaster JWT 修复测试脚本
# 用于测试修复后的JWT认证功能
param(
[string]$BaseUrl = "http://localhost:8080",
[string]$Username = "admin",
[string]$Password = "password"
)
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " GoFaster JWT 修复测试脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 1. 测试健康检查端点
Write-Host "1. 测试健康检查端点..." -ForegroundColor Yellow
try {
$healthResponse = Invoke-RestMethod -Uri "$BaseUrl/health" -Method GET -TimeoutSec 10
Write-Host "✅ 健康检查通过: $($healthResponse.status)" -ForegroundColor Green
} catch {
Write-Host "❌ 健康检查失败: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
Write-Host ""
# 2. 测试登录接口
Write-Host "2. 测试登录接口..." -ForegroundColor Yellow
try {
$loginData = @{
username = $Username
password = $Password
} | ConvertTo-Json
$loginResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/login" -Method POST -Body $loginData -ContentType "application/json" -TimeoutSec 10
if ($loginResponse.data.token) {
Write-Host "✅ 登录成功,获取到token" -ForegroundColor Green
$token = $loginResponse.data.token
Write-Host " Token: $($token.Substring(0, [Math]::Min(50, $token.Length)))..." -ForegroundColor White
# 检查token是否是真正的JWT格式
if ($token -match "^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$") {
Write-Host " ✅ Token格式正确 (JWT格式)" -ForegroundColor Green
} else {
Write-Host " Token格式可能不正确" -ForegroundColor Yellow
}
} else {
Write-Host "❌ 登录失败,未获取到token" -ForegroundColor Red
Write-Host "响应内容: $($loginResponse | ConvertTo-Json)" -ForegroundColor Red
exit 1
}
} catch {
Write-Host "❌ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
Write-Host ""
# 3. 测试用户信息接口
Write-Host "3. 测试用户信息接口..." -ForegroundColor Yellow
try {
$headers = @{
"Authorization" = "Bearer $token"
"Content-Type" = "application/json"
}
$userInfoResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/userinfo" -Method GET -Headers $headers -TimeoutSec 10
if ($userInfoResponse.data) {
Write-Host "✅ 用户信息获取成功" -ForegroundColor Green
Write-Host " 用户ID: $($userInfoResponse.data.id)" -ForegroundColor White
Write-Host " 用户名: $($userInfoResponse.data.username)" -ForegroundColor White
Write-Host " 邮箱: $($userInfoResponse.data.email)" -ForegroundColor White
} else {
Write-Host "❌ 用户信息获取失败" -ForegroundColor Red
Write-Host "响应内容: $($userInfoResponse | ConvertTo-Json)" -ForegroundColor Red
}
} catch {
Write-Host "❌ 用户信息获取失败: $($_.Exception.Message)" -ForegroundColor Red
if ($_.Exception.Response) {
$statusCode = $_.Exception.Response.StatusCode
Write-Host "HTTP状态码: $statusCode" -ForegroundColor Red
}
}
Write-Host ""
# 4. 测试用户列表接口(使用原始管理员路由)
Write-Host "4. 测试用户列表接口(原始管理员路由)..." -ForegroundColor Yellow
try {
$usersUrl = "$BaseUrl/api/auth/admin/users?page=1&pageSize=10"
$usersResponse = Invoke-RestMethod -Uri $usersUrl -Method GET -Headers $headers -TimeoutSec 10
if ($usersResponse.data) {
Write-Host "✅ 用户列表获取成功" -ForegroundColor Green
Write-Host " 用户数量: $($usersResponse.data.Count)" -ForegroundColor White
Write-Host " 总数量: $($usersResponse.total)" -ForegroundColor White
Write-Host " 当前页: $($usersResponse.page)" -ForegroundColor White
Write-Host " 每页大小: $($usersResponse.size)" -ForegroundColor White
} else {
Write-Host "❌ 用户列表获取失败" -ForegroundColor Red
Write-Host "响应内容: $($usersResponse | ConvertTo-Json)" -ForegroundColor Red
}
} catch {
Write-Host "❌ 用户列表获取失败: $($_.Exception.Message)" -ForegroundColor Red
if ($_.Exception.Response) {
$statusCode = $_.Exception.Response.StatusCode
Write-Host "HTTP状态码: $statusCode" -ForegroundColor Red
}
}
Write-Host ""
# 5. 测试角色列表接口
Write-Host "5. 测试角色列表接口..." -ForegroundColor Yellow
try {
$rolesResponse = Invoke-RestMethod -Uri "$BaseUrl/api/auth/admin/roles" -Method GET -Headers $headers -TimeoutSec 10
if ($rolesResponse.data) {
Write-Host "✅ 角色列表获取成功" -ForegroundColor Green
Write-Host " 角色数量: $($rolesResponse.data.Count)" -ForegroundColor White
foreach ($role in $rolesResponse.data) {
Write-Host " - $($role.name) ($($role.code))" -ForegroundColor White
}
} else {
Write-Host "❌ 角色列表获取失败" -ForegroundColor Red
Write-Host "响应内容: $($rolesResponse | ConvertTo-Json)" -ForegroundColor Red
}
} catch {
Write-Host "❌ 角色列表获取失败: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " JWT 修复测试完成" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 显示修复说明
Write-Host "修复内容:" -ForegroundColor Yellow
Write-Host " - 修复了JWT token生成,现在生成真正的JWT格式" -ForegroundColor White
Write-Host " - 修复了GetUserID函数,使其更安全" -ForegroundColor White
Write-Host " - 添加了详细的调试日志" -ForegroundColor White
Write-Host ""
Write-Host "预期结果:" -ForegroundColor Yellow
Write-Host " - 登录后应该获得真正的JWT token" -ForegroundColor White
Write-Host " - 用户信息接口应该能正常访问" -ForegroundColor White
Write-Host " - 用户列表和角色列表应该能正常获取" -ForegroundColor White
Write-Host ""
Write-Host "按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")