128 lines
4.0 KiB
Python
128 lines
4.0 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
登录调试脚本 - 诊断JWT认证问题
|
|||
|
|
|
|||
|
|
运行方法:
|
|||
|
|
cd /Users/jiliu/工作/projects/imeeting/backend
|
|||
|
|
source venv/bin/activate # 激活虚拟环境
|
|||
|
|
python test/test_login_debug.py
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
import requests
|
|||
|
|
import json
|
|||
|
|
|
|||
|
|
# 添加项目根目录到Python路径
|
|||
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||
|
|
|
|||
|
|
BASE_URL = "http://127.0.0.1:8000"
|
|||
|
|
|
|||
|
|
# 禁用代理以避免本地请求被代理
|
|||
|
|
PROXIES = {'http': None, 'https': None}
|
|||
|
|
|
|||
|
|
def test_backend_connection():
|
|||
|
|
"""测试后端连接"""
|
|||
|
|
try:
|
|||
|
|
response = requests.get(f"{BASE_URL}/", proxies=PROXIES)
|
|||
|
|
print(f"✅ 后端服务连接成功: {response.status_code}")
|
|||
|
|
return True
|
|||
|
|
except requests.exceptions.ConnectionError:
|
|||
|
|
print("❌ 无法连接到后端服务")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_login_with_debug(username, password):
|
|||
|
|
"""详细的登录测试"""
|
|||
|
|
print(f"\n=== 测试登录: {username} ===")
|
|||
|
|
|
|||
|
|
login_data = {
|
|||
|
|
"username": username,
|
|||
|
|
"password": password
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
print(f"请求URL: {BASE_URL}/api/auth/login")
|
|||
|
|
print(f"请求数据: {json.dumps(login_data, ensure_ascii=False)}")
|
|||
|
|
|
|||
|
|
response = requests.post(f"{BASE_URL}/api/auth/login", json=login_data, proxies=PROXIES)
|
|||
|
|
|
|||
|
|
print(f"响应状态码: {response.status_code}")
|
|||
|
|
print(f"响应头: {dict(response.headers)}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
user_data = response.json()
|
|||
|
|
print("✅ 登录成功!")
|
|||
|
|
print(f"用户信息: {json.dumps(user_data, ensure_ascii=False, indent=2)}")
|
|||
|
|
return user_data.get("token")
|
|||
|
|
else:
|
|||
|
|
print("❌ 登录失败")
|
|||
|
|
print(f"错误内容: {response.text}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 请求异常: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def test_authenticated_request(token):
|
|||
|
|
"""测试认证请求"""
|
|||
|
|
if not token:
|
|||
|
|
print("❌ 没有有效token,跳过认证测试")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
print(f"\n=== 测试认证请求 ===")
|
|||
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 测试 /api/auth/me
|
|||
|
|
print("测试 /api/auth/me")
|
|||
|
|
response = requests.get(f"{BASE_URL}/api/auth/me", headers=headers, proxies=PROXIES)
|
|||
|
|
print(f"状态码: {response.status_code}")
|
|||
|
|
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print("✅ 认证请求成功")
|
|||
|
|
print(f"用户信息: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
|
|||
|
|
else:
|
|||
|
|
print("❌ 认证请求失败")
|
|||
|
|
print(f"错误: {response.text}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 认证请求异常: {e}")
|
|||
|
|
|
|||
|
|
def check_database_users():
|
|||
|
|
"""检查数据库用户"""
|
|||
|
|
try:
|
|||
|
|
from app.core.database import get_db_connection
|
|||
|
|
|
|||
|
|
print(f"\n=== 检查数据库用户 ===")
|
|||
|
|
with get_db_connection() as connection:
|
|||
|
|
cursor = connection.cursor(dictionary=True)
|
|||
|
|
cursor.execute("SELECT user_id, username, caption, email FROM users LIMIT 10")
|
|||
|
|
users = cursor.fetchall()
|
|||
|
|
|
|||
|
|
print(f"数据库中的用户 (前10个):")
|
|||
|
|
for user in users:
|
|||
|
|
print(f" - ID: {user['user_id']}, 用户名: {user['username']}, 名称: {user['caption']}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 无法访问数据库: {e}")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
print("JWT登录调试工具")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
# 1. 测试后端连接
|
|||
|
|
if not test_backend_connection():
|
|||
|
|
exit(1)
|
|||
|
|
|
|||
|
|
# 2. 检查数据库用户
|
|||
|
|
check_database_users()
|
|||
|
|
|
|||
|
|
# 3. 测试登录
|
|||
|
|
username = input("\n请输入用户名 (默认: mula): ").strip() or "mula"
|
|||
|
|
password = input("请输入密码 (默认: 781126): ").strip() or "781126"
|
|||
|
|
|
|||
|
|
token = test_login_with_debug(username, password)
|
|||
|
|
|
|||
|
|
# 4. 测试认证请求
|
|||
|
|
test_authenticated_request(token)
|
|||
|
|
|
|||
|
|
print("\n=== 调试完成 ===")
|