修改 HTTPDataSource 的认证方式
This commit is contained in:
parent
cfcdaac3e5
commit
24c14bcab2
@ -28,10 +28,10 @@
|
||||
"port": 8081,
|
||||
"username": "dianxin",
|
||||
"password": "dianxin@123",
|
||||
"auth_path": "/api/login",
|
||||
"aircraft_path": "/api/getCurrentFlightPositions",
|
||||
"vehicle_path": "/api/getCurrentVehiclePositions",
|
||||
"traffic_light_path": "/api/getTrafficLightSignals",
|
||||
"auth_path": "/login",
|
||||
"aircraft_path": "/openApi/getCurrentFlightPositions",
|
||||
"vehicle_path": "/openApi/getCurrentVehiclePositions",
|
||||
"traffic_light_path": "/getTrafficLightSignals",
|
||||
"refresh_interval_ms": 1000,
|
||||
"timeout_ms": 5000,
|
||||
"read_timeout_ms": 2000
|
||||
|
||||
@ -409,33 +409,25 @@ bool HTTPDataSource::authenticate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 构造认证请求URL
|
||||
std::string url = "http://" + host_ + ":" + port_ + config_.auth_path;
|
||||
// 构造带参数的认证URL
|
||||
std::string url = "http://" + host_ + ":" + port_ + config_.auth_path +
|
||||
"?username=" + config_.username +
|
||||
"&password=" + config_.password;
|
||||
Logger::debug("Authentication URL: ", url);
|
||||
|
||||
// 构造认证请求体
|
||||
nlohmann::json request = {
|
||||
{"username", config_.username},
|
||||
{"password", config_.password}
|
||||
};
|
||||
|
||||
std::string request_body = request.dump();
|
||||
std::string response;
|
||||
|
||||
Logger::debug("Sending authentication request with username: ", config_.username);
|
||||
Logger::debug("Request body: ", request_body);
|
||||
|
||||
// 设置CURL选项
|
||||
curl_easy_reset(curl_);
|
||||
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl_, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, request_body.c_str());
|
||||
curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, static_cast<long>(request_body.length()));
|
||||
curl_easy_setopt(curl_, CURLOPT_POST, 1L); // 使用POST方法
|
||||
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, ""); // 空的POST数据
|
||||
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &response);
|
||||
|
||||
// 设置请求头
|
||||
struct curl_slist* headers = nullptr;
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
headers = curl_slist_append(headers, "Accept: application/json");
|
||||
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
// 发送请求
|
||||
@ -453,6 +445,7 @@ bool HTTPDataSource::authenticate() {
|
||||
|
||||
if (http_code != 200) {
|
||||
Logger::error("Authentication failed with HTTP code: ", http_code);
|
||||
Logger::error("Response body: ", response);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -479,6 +472,7 @@ bool HTTPDataSource::authenticate() {
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
Logger::error("Failed to parse authentication response: ", e.what());
|
||||
Logger::error("Raw response: ", response);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -426,7 +426,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
ws = new WebSocket('ws://10.0.0.118:8010');
|
||||
ws = new WebSocket('ws://localhost:8010');
|
||||
|
||||
ws.onopen = () => {
|
||||
log('连接成功', 'success');
|
||||
|
||||
@ -744,7 +744,7 @@ def check_auth():
|
||||
return False
|
||||
return True
|
||||
|
||||
@app.route('/api/getCurrentFlightPositions', methods=['GET', 'OPTIONS'])
|
||||
@app.route('/openApi/getCurrentFlightPositions', methods=['GET', 'OPTIONS'])
|
||||
def get_flight_positions():
|
||||
if request.method == 'OPTIONS':
|
||||
return '', 204
|
||||
@ -790,7 +790,7 @@ def switch_traffic_light_state():
|
||||
if old_state != traffic_light_data[1]["state"]:
|
||||
print(f"东路口红绿灯状态切换为: {'绿灯' if traffic_light_data[1]['state'] == 1 else '红灯'}")
|
||||
|
||||
@app.route('/api/getCurrentVehiclePositions', methods=['GET', 'OPTIONS'])
|
||||
@app.route('/openApi/getCurrentVehiclePositions', methods=['GET', 'OPTIONS'])
|
||||
def get_vehicle_positions():
|
||||
if request.method == 'OPTIONS':
|
||||
return '', 204
|
||||
@ -821,7 +821,7 @@ def get_vehicle_positions():
|
||||
print(f"Error in get_vehicle_positions: {str(e)}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route('/api/getTrafficLightSignals', methods=['GET', 'OPTIONS'])
|
||||
@app.route('/getTrafficLightSignals', methods=['GET', 'OPTIONS'])
|
||||
def get_traffic_light_signals():
|
||||
if request.method == 'OPTIONS':
|
||||
return '', 204
|
||||
@ -860,21 +860,27 @@ AUTH_USERNAME = "dianxin"
|
||||
AUTH_PASSWORD = "dianxin@123"
|
||||
AUTH_TOKEN = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzI3ODMwOTAsInVzZXJuYW1lIjoiYWRtaW4ifQ.y9feEL_9NT8UzED9NNkb0Ln6C-PBoufiSHWobWe5vWY"
|
||||
|
||||
@app.route('/api/login', methods=['POST', 'OPTIONS'])
|
||||
@app.route('/login', methods=['POST', 'OPTIONS'])
|
||||
def login():
|
||||
if request.method == 'OPTIONS':
|
||||
return '', 204
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
|
||||
# 从 URL query parameters 中获取用户名和密码
|
||||
username = request.args.get('username')
|
||||
password = request.args.get('password')
|
||||
|
||||
print(f"收到登录请求: username={username}, password={password}")
|
||||
print(f"请求 URL: {request.url}")
|
||||
print(f"请求方法: {request.method}")
|
||||
print(f"请求参数: {request.args}")
|
||||
|
||||
if not username or not password:
|
||||
return jsonify({
|
||||
"status": 400,
|
||||
"msg": "请求格式错误",
|
||||
"msg": "缺少用户名或密码",
|
||||
"data": None
|
||||
}), 400
|
||||
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
if username == AUTH_USERNAME and password == AUTH_PASSWORD:
|
||||
return jsonify({
|
||||
"status": 200,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user