feat(login): 支持发送加密和未加密登录请求
- 添加 send_unencrypted_request 函数用于发送未加密请求 - 添加 send_encrypted_request 函数用于发送加密请求- 重构主流程,通过 USE_ENCRYPTION 开关选择请求类型 - 优化日志输出,区分加密与未加密请求状态 - 统一异常处理逻辑,增强代码健壮性 - 保留原有加密逻辑并整合到新结构中main
parent
3ab5f15b0e
commit
3489709697
|
|
@ -27,55 +27,69 @@ def get_des_encrypt(data: str, _key: str = DEFAULT_KEY, _iv: str = DEFAULT_IV) -
|
|||
return base64.b64encode(encrypted_bytes).decode('utf-8')
|
||||
|
||||
|
||||
# === 构造原始登录数据 ===
|
||||
login_data = {
|
||||
"userId": "admin",
|
||||
"password": "123456"
|
||||
}
|
||||
def send_unencrypted_request(url, data):
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=json.dumps(data))
|
||||
print("🔓 [未加密] Status Code:", response.status_code)
|
||||
print("🔓 [未加密] Response Text:", response.text)
|
||||
result = response.json()
|
||||
token = result.get("data", {}).get("token")
|
||||
if token:
|
||||
print("✅ [未加密] 登录成功,Token:", token)
|
||||
else:
|
||||
print("❌ [未加密] 未获取到 Token,响应内容:", result)
|
||||
except requests.RequestException as e:
|
||||
print("🌐 [未加密] 请求异常:", e)
|
||||
except json.JSONDecodeError:
|
||||
print("🧩 [未加密] 响应不是合法的 JSON 格式")
|
||||
|
||||
# 转为 JSON 字符串(注意:不要加空格,确保与前端一致)
|
||||
data_str = json.dumps(login_data, separators=(',', ':')) # 紧凑格式,无空格
|
||||
|
||||
# 加密
|
||||
encrypted_data = get_des_encrypt(data_str, DEFAULT_KEY, DEFAULT_IV)
|
||||
|
||||
# === 构造最终请求 payload ===
|
||||
payload = {
|
||||
def send_encrypted_request(url, data):
|
||||
# 将原始数据转为紧凑 JSON 字符串
|
||||
data_str = json.dumps(data, separators=(',', ':'))
|
||||
# 加密
|
||||
encrypted_data = get_des_encrypt(data_str, DEFAULT_KEY, DEFAULT_IV)
|
||||
# 构造最终 payload
|
||||
payload = {
|
||||
"encType": 0,
|
||||
"x_flag": "",
|
||||
"data": encrypted_data # 注意:这里现在是加密后的字符串,不是 dict!
|
||||
}
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"data": encrypted_data
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
# 接口地址
|
||||
url = "http://127.0.0.1:8084/c/v1/wellMudLogAndWireLine/system/privateManager/userLoginAggregate/other/login"
|
||||
|
||||
try:
|
||||
# 发送 POST 请求
|
||||
}
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
||||
|
||||
# 打印调试信息(可选)
|
||||
print("Status Code:", response.status_code)
|
||||
print("Response Text:", response.text)
|
||||
|
||||
# 解析响应
|
||||
print("🔒 [已加密] Status Code:", response.status_code)
|
||||
print("🔒 [已加密] Response Text:", response.text)
|
||||
result = response.json()
|
||||
|
||||
# 正确获取 token(你原来的写法 result.post(...) 是错误的)
|
||||
token = result.get("data", {}).get("token")
|
||||
|
||||
if token:
|
||||
print("✅ 登录成功,Token:", token)
|
||||
print("✅ [已加密] 登录成功,Token:", token)
|
||||
else:
|
||||
print("❌ 未获取到 Token,响应内容:", result)
|
||||
print("❌ [已加密] 未获取到 Token,响应内容:", result)
|
||||
except requests.RequestException as e:
|
||||
print("🌐 [已加密] 请求异常:", e)
|
||||
except json.JSONDecodeError:
|
||||
print("🧩 [已加密] 响应不是合法的 JSON 格式")
|
||||
except ValueError as ve:
|
||||
print("⚠️ [已加密] 加密参数错误:", ve)
|
||||
|
||||
except requests.RequestException as e:
|
||||
print("🌐 请求异常:", e)
|
||||
except json.JSONDecodeError:
|
||||
print("🧩 响应不是合法的 JSON 格式")
|
||||
except ValueError as ve:
|
||||
print("⚠️ 加密参数错误:", ve)
|
||||
|
||||
if __name__ == '__main__':
|
||||
url = "http://127.0.0.1:8084/c/v1/wellMudLogAndWireLine/system/privateManager/userLoginAggregate/other/login"
|
||||
login_data = {
|
||||
"userId": "admin",
|
||||
"password": "123456"
|
||||
}
|
||||
|
||||
# ========== 选择模式 ==========
|
||||
USE_ENCRYPTION = False # 改为 False 则发送未加密请求
|
||||
|
||||
if USE_ENCRYPTION:
|
||||
send_encrypted_request(url, login_data)
|
||||
else:
|
||||
send_unencrypted_request(url, login_data)
|
||||
Loading…
Reference in New Issue