移除了登录后端库的Qt依赖
This commit is contained in:
parent
53e2d5c24f
commit
d92947628b
@ -2,9 +2,6 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
|
|
||||||
project(Login LANGUAGES CXX)
|
project(Login LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_AUTOUIC ON)
|
|
||||||
set(CMAKE_AUTOMOC ON)
|
|
||||||
set(CMAKE_AUTORCC ON)
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
@ -15,13 +12,10 @@ else()
|
|||||||
message(FATAL_ERROR "Environment variable XNCore is not set.")
|
message(FATAL_ERROR "Environment variable XNCore is not set.")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Sql)
|
# 查找依赖包
|
||||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Sql)
|
find_package(SQLite3 REQUIRED)
|
||||||
|
find_package(OpenSSL REQUIRED)
|
||||||
# 添加测试程序
|
find_package(nlohmann_json 3.9.1 REQUIRED)
|
||||||
add_executable(test_password
|
|
||||||
test_encrypt/test_password.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
add_library(Login SHARED
|
add_library(Login SHARED
|
||||||
Login_global.h
|
Login_global.h
|
||||||
@ -30,15 +24,13 @@ add_library(Login SHARED
|
|||||||
|
|
||||||
# 添加头文件搜索路径
|
# 添加头文件搜索路径
|
||||||
target_include_directories(Login PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(Login PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
target_include_directories(test_password PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
|
||||||
|
|
||||||
|
# 链接依赖库
|
||||||
target_link_libraries(Login PRIVATE
|
target_link_libraries(Login PRIVATE
|
||||||
Qt${QT_VERSION_MAJOR}::Core
|
SQLite::SQLite3
|
||||||
Qt${QT_VERSION_MAJOR}::Sql)
|
OpenSSL::SSL
|
||||||
target_link_libraries(test_password PRIVATE
|
OpenSSL::Crypto
|
||||||
Qt${QT_VERSION_MAJOR}::Core
|
nlohmann_json::nlohmann_json
|
||||||
Qt${QT_VERSION_MAJOR}::Sql
|
|
||||||
Login
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_definitions(Login PRIVATE LOGIN_LIBRARY)
|
target_compile_definitions(Login PRIVATE LOGIN_LIBRARY)
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
#ifndef LOGIN_GLOBAL_H
|
#ifndef LOGIN_GLOBAL_H
|
||||||
#define LOGIN_GLOBAL_H
|
#define LOGIN_GLOBAL_H
|
||||||
|
|
||||||
#include <QtCore/qglobal.h>
|
#if defined(_WIN32)
|
||||||
|
#if defined(LOGIN_LIBRARY)
|
||||||
#if defined(LOGIN_LIBRARY)
|
#define LOGIN_EXPORT __declspec(dllexport)
|
||||||
# define LOGIN_EXPORT Q_DECL_EXPORT
|
#else
|
||||||
|
#define LOGIN_EXPORT __declspec(dllimport)
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
# define LOGIN_EXPORT Q_DECL_IMPORT
|
#if defined(LOGIN_LIBRARY)
|
||||||
|
#define LOGIN_EXPORT __attribute__((visibility("default")))
|
||||||
|
#else
|
||||||
|
#define LOGIN_EXPORT
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // LOGIN_GLOBAL_H
|
#endif // LOGIN_GLOBAL_H
|
||||||
|
554
Login/login.cpp
554
Login/login.cpp
@ -1,257 +1,201 @@
|
|||||||
#include "Login_global.h"
|
#include "Login_global.h"
|
||||||
#include <QString>
|
|
||||||
#include <QCryptographicHash>
|
|
||||||
#include <QProcessEnvironment>
|
|
||||||
#include <QDir>
|
|
||||||
#include <QSqlDatabase>
|
|
||||||
#include <QSqlQuery>
|
|
||||||
#include <QSqlError>
|
|
||||||
#include <QSqlRecord>
|
|
||||||
#include <QJsonObject>
|
|
||||||
#include <QJsonDocument>
|
|
||||||
#include <QCoreApplication>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <QDateTime>
|
#include <chrono>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
// 全局变量用于跟踪是否已初始化
|
using json = nlohmann::json;
|
||||||
static bool g_isInitialized = false;
|
namespace fs = std::filesystem;
|
||||||
static QCoreApplication *g_app = nullptr;
|
|
||||||
|
|
||||||
// 初始化Qt环境
|
std::string generateSalt(const std::string& username)
|
||||||
void initializeQt()
|
|
||||||
{
|
|
||||||
if (!g_isInitialized) {
|
|
||||||
static int argc = 1;
|
|
||||||
static char *argv[] = {const_cast<char *>("Login")};
|
|
||||||
g_app = new QCoreApplication(argc, argv);
|
|
||||||
g_isInitialized = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清理Qt环境
|
|
||||||
void cleanupQt()
|
|
||||||
{
|
|
||||||
if (g_isInitialized && g_app) {
|
|
||||||
delete g_app;
|
|
||||||
g_app = nullptr;
|
|
||||||
g_isInitialized = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QString generateSalt(const QString &username)
|
|
||||||
{
|
{
|
||||||
// 使用用户名生成盐值
|
// 使用用户名生成盐值
|
||||||
// 可以根据需要添加额外的静态字符串来增加复杂度
|
|
||||||
return username + "XNSim_Salt_Key";
|
return username + "XNSim_Salt_Key";
|
||||||
}
|
}
|
||||||
|
|
||||||
QString encryptPassword(const QString &password, const QString &salt)
|
std::string encryptPassword(const std::string& password, const std::string& salt)
|
||||||
{
|
{
|
||||||
// 将密码和盐值组合
|
// 将密码和盐值组合
|
||||||
QByteArray saltedPassword = password.toUtf8();
|
std::string saltedPassword = password;
|
||||||
|
if (!salt.empty()) {
|
||||||
// 如果提供了盐值,则添加到密码中
|
saltedPassword += salt;
|
||||||
if (!salt.isEmpty()) {
|
|
||||||
saltedPassword.append(salt.toUtf8());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用SHA-256算法对加盐密码进行加密
|
// 使用SHA-256算法对加盐密码进行加密
|
||||||
QByteArray hashedPassword =
|
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||||
QCryptographicHash::hash(saltedPassword, QCryptographicHash::Sha256);
|
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
|
||||||
return hashedPassword.toHex();
|
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
|
||||||
|
EVP_DigestUpdate(ctx, saltedPassword.c_str(), saltedPassword.length());
|
||||||
|
EVP_DigestFinal_ex(ctx, hash, NULL);
|
||||||
|
EVP_MD_CTX_free(ctx);
|
||||||
|
|
||||||
|
// 转换为十六进制字符串
|
||||||
|
std::stringstream ss;
|
||||||
|
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||||
|
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
|
||||||
|
}
|
||||||
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" LOGIN_EXPORT int validateUser(const void *username_buffer, size_t username_length,
|
extern "C" LOGIN_EXPORT int validateUser(const void* username_buffer, size_t username_length,
|
||||||
const void *password_buffer, size_t password_length)
|
const void* password_buffer, size_t password_length)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
std::string username_str(static_cast<const char*>(username_buffer), username_length);
|
||||||
|
std::string password_str(static_cast<const char*>(password_buffer), password_length);
|
||||||
|
|
||||||
const char *username_data = static_cast<const char *>(username_buffer);
|
if (username_str.empty() || password_str.empty()) {
|
||||||
const char *password_data = static_cast<const char *>(password_buffer);
|
|
||||||
|
|
||||||
QByteArray username_bytes(username_data, username_length);
|
|
||||||
QByteArray password_bytes(password_data, password_length);
|
|
||||||
|
|
||||||
QString username_str = QString::fromUtf8(username_bytes);
|
|
||||||
QString password_str = QString::fromUtf8(password_bytes);
|
|
||||||
|
|
||||||
if (username_str.isEmpty() || password_str.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString salt = generateSalt(username_str);
|
std::string salt = generateSalt(username_str);
|
||||||
QString encryptedPassword = encryptPassword(password_str, salt);
|
std::string encryptedPassword = encryptPassword(password_str, salt);
|
||||||
|
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
// 获取环境变量
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
|
if (!xnCorePath) {
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
|
|
||||||
|
sqlite3* db;
|
||||||
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int userId = -1;
|
int userId = -1;
|
||||||
{
|
sqlite3_stmt* stmt;
|
||||||
// 创建一个独立的作用域来管理数据库连接
|
const char* queryStr = "SELECT * FROM users WHERE username = ? AND password = ?";
|
||||||
QString connectionName =
|
|
||||||
QString("userauth_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
|
||||||
{
|
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
|
||||||
db.setDatabaseName(dbPath);
|
|
||||||
|
|
||||||
if (!db.open()) {
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
sqlite3_bind_text(stmt, 1, username_str.c_str(), -1, SQLITE_STATIC);
|
||||||
return -1;
|
sqlite3_bind_text(stmt, 2, encryptedPassword.c_str(), -1, SQLITE_STATIC);
|
||||||
|
|
||||||
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
|
userId = sqlite3_column_int(stmt, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery query(db);
|
sqlite3_finalize(stmt);
|
||||||
QString queryStr = "SELECT * FROM users WHERE username = ? AND password = ?";
|
|
||||||
query.prepare(queryStr);
|
|
||||||
query.addBindValue(username_str);
|
|
||||||
query.addBindValue(encryptedPassword);
|
|
||||||
|
|
||||||
if (query.exec() && query.next()) {
|
|
||||||
userId = query.value(0).toInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
query.finish();
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
return userId;
|
return userId;
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception&) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" LOGIN_EXPORT const char *getUserInfo(int user_id)
|
extern "C" LOGIN_EXPORT const char* getUserInfo(int user_id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
|
if (!xnCorePath) {
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
|
||||||
|
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
|
|
||||||
char *result = nullptr;
|
sqlite3* db;
|
||||||
{
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
// 创建一个独立的作用域来管理数据库连接
|
|
||||||
QString connectionName =
|
|
||||||
QString("userinfo_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
|
||||||
{
|
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
|
||||||
db.setDatabaseName(dbPath);
|
|
||||||
|
|
||||||
if (!db.open()) {
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery query(db);
|
char* result = nullptr;
|
||||||
query.prepare("SELECT * FROM users WHERE id = ?");
|
sqlite3_stmt* stmt;
|
||||||
query.addBindValue(user_id);
|
const char* queryStr = "SELECT * FROM users WHERE id = ?";
|
||||||
|
|
||||||
if (query.exec() && query.next()) {
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
QJsonObject userInfo;
|
sqlite3_bind_int(stmt, 1, user_id);
|
||||||
userInfo["id"] = query.value(0).toInt();
|
|
||||||
userInfo["username"] = query.value(1).toString();
|
|
||||||
userInfo["access_level"] = query.value(3).toInt();
|
|
||||||
userInfo["full_name"] = query.value(4).toString();
|
|
||||||
userInfo["phone"] = query.value(5).toString();
|
|
||||||
userInfo["email"] = query.value(6).toString();
|
|
||||||
userInfo["department"] = query.value(7).toString();
|
|
||||||
userInfo["position"] = query.value(8).toString();
|
|
||||||
|
|
||||||
QJsonDocument doc(userInfo);
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
QByteArray jsonData = doc.toJson(QJsonDocument::Compact);
|
json userInfo;
|
||||||
|
userInfo["id"] = sqlite3_column_int(stmt, 0);
|
||||||
|
userInfo["username"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||||
|
userInfo["access_level"] = sqlite3_column_int(stmt, 3);
|
||||||
|
userInfo["full_name"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4));
|
||||||
|
userInfo["phone"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 5));
|
||||||
|
userInfo["email"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 6));
|
||||||
|
userInfo["department"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
|
||||||
|
userInfo["position"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8));
|
||||||
|
|
||||||
|
std::string jsonData = userInfo.dump();
|
||||||
result = new char[jsonData.size() + 1];
|
result = new char[jsonData.size() + 1];
|
||||||
std::strcpy(result, jsonData.constData());
|
std::strcpy(result, jsonData.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
query.finish();
|
sqlite3_finalize(stmt);
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception&) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" LOGIN_EXPORT void freeUserInfo(const char *ptr)
|
extern "C" LOGIN_EXPORT void freeUserInfo(const char* ptr)
|
||||||
{
|
{
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
delete[] ptr;
|
delete[] ptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出清理函数
|
|
||||||
extern "C" LOGIN_EXPORT void cleanup()
|
extern "C" LOGIN_EXPORT void cleanup()
|
||||||
{
|
{
|
||||||
cleanupQt();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查用户名是否已存在
|
// 检查用户名是否已存在
|
||||||
int checkUsernameExists(const void *username_buffer, size_t username_length)
|
int checkUsernameExists(const void *username_buffer, size_t username_length)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
|
||||||
|
|
||||||
const char *username_data = static_cast<const char *>(username_buffer);
|
const char *username_data = static_cast<const char *>(username_buffer);
|
||||||
QByteArray username_bytes(username_data, username_length);
|
std::string username_str(username_data, username_length);
|
||||||
QString username_str = QString::fromUtf8(username_bytes);
|
|
||||||
|
|
||||||
if (username_str.isEmpty()) {
|
if (username_str.empty()) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
if (!xnCorePath) {
|
||||||
|
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
QString connectionName = QString("usercheck_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
std::string connectionName = "usercheck_" + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
|
||||||
|
|
||||||
int result = -1;
|
int result = -1;
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
sqlite3* db;
|
||||||
db.setDatabaseName(dbPath);
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
|
|
||||||
if (!db.open()) {
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery query(db);
|
sqlite3_stmt* stmt;
|
||||||
query.prepare("SELECT COUNT(*) FROM users WHERE username = ?");
|
const char* queryStr = "SELECT COUNT(*) FROM users WHERE username = ?";
|
||||||
query.addBindValue(username_str);
|
|
||||||
|
|
||||||
if (query.exec() && query.next()) {
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
result = query.value(0).toInt() > 0 ? 1 : 0;
|
sqlite3_bind_text(stmt, 1, username_str.c_str(), -1, SQLITE_STATIC);
|
||||||
|
|
||||||
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
|
result = sqlite3_column_int(stmt, 0) > 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
query.finish();
|
sqlite3_finalize(stmt);
|
||||||
db.close();
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
}
|
}
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception &) {
|
||||||
@ -265,25 +209,19 @@ extern "C" LOGIN_EXPORT int registerUser(const void *username_buffer, size_t use
|
|||||||
const void *userinfo_buffer, size_t userinfo_length)
|
const void *userinfo_buffer, size_t userinfo_length)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
|
||||||
|
|
||||||
// 转换输入参数
|
// 转换输入参数
|
||||||
const char *username_data = static_cast<const char *>(username_buffer);
|
const char *username_data = static_cast<const char *>(username_buffer);
|
||||||
const char *password_data = static_cast<const char *>(password_buffer);
|
const char *password_data = static_cast<const char *>(password_buffer);
|
||||||
const char *userinfo_data = static_cast<const char *>(userinfo_buffer);
|
const char *userinfo_data = static_cast<const char *>(userinfo_buffer);
|
||||||
|
|
||||||
QByteArray username_bytes(username_data, username_length);
|
std::string username_str(username_data, username_length);
|
||||||
QByteArray password_bytes(password_data, password_length);
|
std::string password_str(password_data, password_length);
|
||||||
QByteArray userinfo_bytes(userinfo_data, userinfo_length);
|
|
||||||
|
|
||||||
QString username_str = QString::fromUtf8(username_bytes);
|
|
||||||
QString password_str = QString::fromUtf8(password_bytes);
|
|
||||||
|
|
||||||
// 验证用户名和密码非空
|
// 验证用户名和密码非空
|
||||||
if (username_str.isEmpty()) {
|
if (username_str.empty()) {
|
||||||
return -4; // 用户名为空
|
return -4; // 用户名为空
|
||||||
}
|
}
|
||||||
if (password_str.isEmpty()) {
|
if (password_str.empty()) {
|
||||||
return -5; // 密码为空
|
return -5; // 密码为空
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,61 +231,60 @@ extern "C" LOGIN_EXPORT int registerUser(const void *username_buffer, size_t use
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 解析用户信息JSON
|
// 解析用户信息JSON
|
||||||
QJsonDocument userInfoDoc = QJsonDocument::fromJson(userinfo_bytes);
|
json userInfo;
|
||||||
if (!userInfoDoc.isObject()) {
|
try {
|
||||||
return -3; // 无效的用户信息格式
|
userInfo = json::parse(std::string(userinfo_data, userinfo_length));
|
||||||
|
} catch (const json::parse_error&) {
|
||||||
|
return -3; // Invalid user info format
|
||||||
}
|
}
|
||||||
QJsonObject userInfo = userInfoDoc.object();
|
|
||||||
|
|
||||||
// 验证权限级别
|
// 验证权限级别
|
||||||
int accessLevel = 0;
|
int accessLevel = 0;
|
||||||
|
|
||||||
// 生成加密密码
|
// 生成加密密码
|
||||||
QString salt = generateSalt(username_str);
|
std::string salt = generateSalt(username_str);
|
||||||
QString encryptedPassword = encryptPassword(password_str, salt);
|
std::string encryptedPassword = encryptPassword(password_str, salt);
|
||||||
|
|
||||||
// 连接数据库
|
// 连接数据库
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
if (!xnCorePath) {
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
QString connectionName = QString("userreg_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
std::string connectionName = "userreg_" + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
|
||||||
|
|
||||||
int newUserId = -1;
|
int newUserId = -1;
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
sqlite3* db;
|
||||||
db.setDatabaseName(dbPath);
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
|
|
||||||
if (!db.open()) {
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery query(db);
|
sqlite3_stmt* stmt;
|
||||||
query.prepare("INSERT INTO users (username, password, access_level, full_name, phone, "
|
const char* queryStr = "INSERT INTO users (username, password, access_level, full_name, phone, "
|
||||||
"email, department, position) "
|
"email, department, position) "
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
|
|
||||||
query.addBindValue(username_str);
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
query.addBindValue(encryptedPassword);
|
sqlite3_bind_text(stmt, 1, username_str.c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(accessLevel);
|
sqlite3_bind_text(stmt, 2, encryptedPassword.c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["full_name"].toString());
|
sqlite3_bind_int(stmt, 3, accessLevel);
|
||||||
query.addBindValue(userInfo["phone"].toString());
|
sqlite3_bind_text(stmt, 4, userInfo["full_name"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["email"].toString());
|
sqlite3_bind_text(stmt, 5, userInfo["phone"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["department"].toString());
|
sqlite3_bind_text(stmt, 6, userInfo["email"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["position"].toString());
|
sqlite3_bind_text(stmt, 7, userInfo["department"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
|
sqlite3_bind_text(stmt, 8, userInfo["position"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
|
|
||||||
if (query.exec()) {
|
if (sqlite3_step(stmt) == SQLITE_DONE) {
|
||||||
newUserId = query.lastInsertId().toInt();
|
newUserId = sqlite3_last_insert_rowid(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
query.finish();
|
sqlite3_finalize(stmt);
|
||||||
db.close();
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
}
|
}
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return newUserId;
|
return newUserId;
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception &) {
|
||||||
@ -362,85 +299,79 @@ extern "C" LOGIN_EXPORT int changePassword(int user_id, const void *old_password
|
|||||||
size_t new_password_length)
|
size_t new_password_length)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
|
||||||
|
|
||||||
const char *old_password_data = static_cast<const char *>(old_password_buffer);
|
const char *old_password_data = static_cast<const char *>(old_password_buffer);
|
||||||
const char *new_password_data = static_cast<const char *>(new_password_buffer);
|
const char *new_password_data = static_cast<const char *>(new_password_buffer);
|
||||||
|
|
||||||
QByteArray old_password_bytes(old_password_data, old_password_length);
|
std::string old_password_str(old_password_data, old_password_length);
|
||||||
QByteArray new_password_bytes(new_password_data, new_password_length);
|
std::string new_password_str(new_password_data, new_password_length);
|
||||||
|
|
||||||
QString old_password_str = QString::fromUtf8(old_password_bytes);
|
if (old_password_str.empty() || new_password_str.empty()) {
|
||||||
QString new_password_str = QString::fromUtf8(new_password_bytes);
|
|
||||||
|
|
||||||
if (old_password_str.isEmpty() || new_password_str.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
if (!xnCorePath) {
|
||||||
|
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
QString connectionName = QString("changepwd_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
std::string connectionName = "changepwd_" + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
|
||||||
|
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
sqlite3* db;
|
||||||
db.setDatabaseName(dbPath);
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
|
|
||||||
if (!db.open()) {
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首先验证旧密码
|
// 首先验证旧密码
|
||||||
QSqlQuery query(db);
|
sqlite3_stmt* stmt;
|
||||||
query.prepare("SELECT username, password FROM users WHERE id = ?");
|
const char* queryStr = "SELECT username, password FROM users WHERE id = ?";
|
||||||
query.addBindValue(user_id);
|
|
||||||
|
|
||||||
if (!query.exec() || !query.next()) {
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
db.close();
|
sqlite3_bind_int(stmt, 1, user_id);
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return -2; // 用户不存在
|
|
||||||
}
|
|
||||||
|
|
||||||
QString username = query.value(0).toString();
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
QString storedPassword = query.value(1).toString();
|
std::string username = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||||
|
std::string storedPassword = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||||
|
|
||||||
// 验证旧密码
|
// 验证旧密码
|
||||||
QString salt = generateSalt(username);
|
std::string salt = generateSalt(username);
|
||||||
QString encryptedOldPassword = encryptPassword(old_password_str, salt);
|
std::string encryptedOldPassword = encryptPassword(old_password_str, salt);
|
||||||
|
|
||||||
if (encryptedOldPassword != storedPassword) {
|
if (encryptedOldPassword != storedPassword) {
|
||||||
db.close();
|
sqlite3_finalize(stmt);
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
sqlite3_close(db);
|
||||||
return -3; // 旧密码错误
|
return -3; // 旧密码错误
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成新的加密密码
|
// 生成新的加密密码
|
||||||
QString encryptedNewPassword = encryptPassword(new_password_str, salt);
|
std::string encryptedNewPassword = encryptPassword(new_password_str, salt);
|
||||||
|
|
||||||
// 更新密码
|
// 更新密码
|
||||||
QSqlQuery updateQuery(db);
|
sqlite3_finalize(stmt);
|
||||||
updateQuery.prepare("UPDATE users SET password = ? WHERE id = ?");
|
stmt = nullptr;
|
||||||
updateQuery.addBindValue(encryptedNewPassword);
|
queryStr = "UPDATE users SET password = ? WHERE id = ?";
|
||||||
updateQuery.addBindValue(user_id);
|
|
||||||
|
|
||||||
if (!updateQuery.exec()) {
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
db.close();
|
sqlite3_bind_text(stmt, 1, encryptedNewPassword.c_str(), -1, SQLITE_STATIC);
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
sqlite3_bind_int(stmt, 2, user_id);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.close();
|
if (sqlite3_step(stmt) == SQLITE_DONE) {
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
sqlite3_finalize(stmt);
|
||||||
|
sqlite3_close(db);
|
||||||
return 1; // 密码修改成功
|
return 1; // 密码修改成功
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
sqlite3_finalize(stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1; // Default error return
|
||||||
|
} catch (const std::exception&) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -450,71 +381,70 @@ extern "C" LOGIN_EXPORT int updateUserInfo(int user_id, const void *userinfo_buf
|
|||||||
size_t userinfo_length)
|
size_t userinfo_length)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
|
||||||
|
|
||||||
const char *userinfo_data = static_cast<const char *>(userinfo_buffer);
|
const char *userinfo_data = static_cast<const char *>(userinfo_buffer);
|
||||||
QByteArray userinfo_bytes(userinfo_data, userinfo_length);
|
std::string userinfo_str(userinfo_data, userinfo_length);
|
||||||
|
|
||||||
// 解析用户信息JSON
|
// 解析用户信息JSON
|
||||||
QJsonDocument userInfoDoc = QJsonDocument::fromJson(userinfo_bytes);
|
json userInfo;
|
||||||
if (!userInfoDoc.isObject()) {
|
try {
|
||||||
return -1; // 无效的用户信息格式
|
userInfo = json::parse(userinfo_str);
|
||||||
|
} catch (const json::parse_error&) {
|
||||||
|
return -1; // Invalid user info format
|
||||||
}
|
}
|
||||||
QJsonObject userInfo = userInfoDoc.object();
|
|
||||||
|
|
||||||
// 连接数据库
|
// 连接数据库
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
if (!xnCorePath) {
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
QString connectionName = QString("userupdate_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
std::string connectionName = "userupdate_" + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
|
||||||
|
|
||||||
int result = -1;
|
int result = -1;
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
sqlite3* db;
|
||||||
db.setDatabaseName(dbPath);
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
|
|
||||||
if (!db.open()) {
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首先检查用户是否存在
|
// 首先检查用户是否存在
|
||||||
QSqlQuery checkQuery(db);
|
sqlite3_stmt* stmt;
|
||||||
checkQuery.prepare("SELECT id FROM users WHERE id = ?");
|
const char* checkQueryStr = "SELECT id FROM users WHERE id = ?";
|
||||||
checkQuery.addBindValue(user_id);
|
|
||||||
|
|
||||||
if (!checkQuery.exec() || !checkQuery.next()) {
|
if (sqlite3_prepare_v2(db, checkQueryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
checkQuery.finish();
|
sqlite3_bind_int(stmt, 1, user_id);
|
||||||
db.close();
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
sqlite3_close(db);
|
||||||
return -2; // 用户不存在
|
return -2; // 用户不存在
|
||||||
}
|
}
|
||||||
checkQuery.finish();
|
sqlite3_finalize(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
QSqlQuery query(db);
|
stmt = nullptr;
|
||||||
query.prepare("UPDATE users SET full_name = ?, phone = ?, email = ?, department = ?, "
|
const char* queryStr = "UPDATE users SET full_name = ?, phone = ?, email = ?, department = ?, "
|
||||||
"position = ? "
|
"position = ? "
|
||||||
"WHERE id = ?");
|
"WHERE id = ?";
|
||||||
|
|
||||||
query.addBindValue(userInfo["full_name"].toString());
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
query.addBindValue(userInfo["phone"].toString());
|
sqlite3_bind_text(stmt, 1, userInfo["full_name"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["email"].toString());
|
sqlite3_bind_text(stmt, 2, userInfo["phone"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["department"].toString());
|
sqlite3_bind_text(stmt, 3, userInfo["email"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(userInfo["position"].toString());
|
sqlite3_bind_text(stmt, 4, userInfo["department"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
query.addBindValue(user_id);
|
sqlite3_bind_text(stmt, 5, userInfo["position"].get<std::string>().c_str(), -1, SQLITE_STATIC);
|
||||||
|
sqlite3_bind_int(stmt, 6, user_id);
|
||||||
|
|
||||||
if (query.exec()) {
|
if (sqlite3_step(stmt) == SQLITE_DONE) {
|
||||||
result = 1; // 更新成功
|
result = 1; // 更新成功
|
||||||
}
|
}
|
||||||
|
|
||||||
query.finish();
|
sqlite3_finalize(stmt);
|
||||||
db.close();
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
}
|
}
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception &) {
|
||||||
@ -526,60 +456,58 @@ extern "C" LOGIN_EXPORT int updateUserInfo(int user_id, const void *userinfo_buf
|
|||||||
extern "C" LOGIN_EXPORT int updateUserAccessLevel(int user_id, int access_level)
|
extern "C" LOGIN_EXPORT int updateUserAccessLevel(int user_id, int access_level)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeQt();
|
|
||||||
|
|
||||||
// 验证权限级别
|
// 验证权限级别
|
||||||
if (access_level < 0 || access_level >= 3) {
|
if (access_level < 0 || access_level >= 3) {
|
||||||
return -3; // 无效的权限级别
|
return -3; // 无效的权限级别
|
||||||
}
|
}
|
||||||
|
|
||||||
// 连接数据库
|
// 连接数据库
|
||||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
const char* xnCorePath = std::getenv("XNCore");
|
||||||
QString xnCorePath = env.value("XNCore", "");
|
if (!xnCorePath) {
|
||||||
if (xnCorePath.isEmpty()) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString dbPath = QDir(xnCorePath).filePath("database/XNSim.db");
|
fs::path dbPath = fs::path(xnCorePath) / "database" / "XNSim.db";
|
||||||
QString connectionName = QString("useraccess_%1").arg(QDateTime::currentMSecsSinceEpoch());
|
std::string connectionName = "useraccess_" + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
|
||||||
|
|
||||||
int result = -1;
|
int result = -1;
|
||||||
{
|
{
|
||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
sqlite3* db;
|
||||||
db.setDatabaseName(dbPath);
|
if (sqlite3_open(dbPath.string().c_str(), &db) != SQLITE_OK) {
|
||||||
|
|
||||||
if (!db.open()) {
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 首先检查用户是否存在
|
// 首先检查用户是否存在
|
||||||
QSqlQuery checkQuery(db);
|
sqlite3_stmt* stmt;
|
||||||
checkQuery.prepare("SELECT id FROM users WHERE id = ?");
|
const char* checkQueryStr = "SELECT id FROM users WHERE id = ?";
|
||||||
checkQuery.addBindValue(user_id);
|
|
||||||
|
|
||||||
if (!checkQuery.exec() || !checkQuery.next()) {
|
if (sqlite3_prepare_v2(db, checkQueryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
checkQuery.finish();
|
sqlite3_bind_int(stmt, 1, user_id);
|
||||||
db.close();
|
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
sqlite3_close(db);
|
||||||
return -2; // 用户不存在
|
return -2; // 用户不存在
|
||||||
}
|
}
|
||||||
checkQuery.finish();
|
sqlite3_finalize(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
QSqlQuery query(db);
|
stmt = nullptr;
|
||||||
query.prepare("UPDATE users SET access_level = ? WHERE id = ?");
|
const char* queryStr = "UPDATE users SET access_level = ? WHERE id = ?";
|
||||||
|
|
||||||
query.addBindValue(access_level);
|
if (sqlite3_prepare_v2(db, queryStr, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||||
query.addBindValue(user_id);
|
sqlite3_bind_int(stmt, 1, access_level);
|
||||||
|
sqlite3_bind_int(stmt, 2, user_id);
|
||||||
|
|
||||||
if (query.exec()) {
|
if (sqlite3_step(stmt) == SQLITE_DONE) {
|
||||||
result = 1; // 更新成功
|
result = 1; // 更新成功
|
||||||
}
|
}
|
||||||
|
|
||||||
query.finish();
|
sqlite3_finalize(stmt);
|
||||||
db.close();
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
}
|
}
|
||||||
QSqlDatabase::removeDatabase(connectionName);
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception &) {
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
|
||||||
project(TestPasswordEncryption)
|
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
|
||||||
set(CMAKE_AUTOMOC ON)
|
|
||||||
set(CMAKE_AUTORCC ON)
|
|
||||||
set(CMAKE_AUTOUIC ON)
|
|
||||||
|
|
||||||
# 查找Qt包
|
|
||||||
find_package(Qt6 COMPONENTS Core REQUIRED)
|
|
||||||
|
|
||||||
# 设置Login库的路径
|
|
||||||
link_directories(${CMAKE_SOURCE_DIR}/../build)
|
|
||||||
|
|
||||||
# 添加头文件路径
|
|
||||||
include_directories(
|
|
||||||
${CMAKE_SOURCE_DIR}/..
|
|
||||||
${Qt6Core_INCLUDE_DIRS}
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(test_password test_password.cpp)
|
|
||||||
target_link_libraries(test_password
|
|
||||||
Qt6::Core
|
|
||||||
Login
|
|
||||||
)
|
|
@ -1,34 +0,0 @@
|
|||||||
#include <QCoreApplication>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <string>
|
|
||||||
#include "Login_global.h"
|
|
||||||
|
|
||||||
// 声明login库中的验证函数
|
|
||||||
extern "C" LOGIN_EXPORT int validateUser(const void *username_buffer, size_t username_length,
|
|
||||||
const void *password_buffer, size_t password_length);
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
QCoreApplication a(argc, argv);
|
|
||||||
|
|
||||||
// 测试用户名和密码
|
|
||||||
QString username_qstr = QString("admin");
|
|
||||||
QString password_qstr = QString("123456");
|
|
||||||
|
|
||||||
QByteArray username_utf8 = username_qstr.toUtf8();
|
|
||||||
QByteArray password_utf8 = password_qstr.toUtf8();
|
|
||||||
|
|
||||||
qDebug() << "开始验证登录...";
|
|
||||||
qDebug() << "用户名:" << username_qstr;
|
|
||||||
qDebug() << "密码:" << password_qstr;
|
|
||||||
|
|
||||||
int accessLevel = validateUser(username_utf8.constData(), username_utf8.size(),
|
|
||||||
password_utf8.constData(), password_utf8.size());
|
|
||||||
|
|
||||||
if (accessLevel >= 0) {
|
|
||||||
qDebug() << "登录成功!用户权限级别:" << accessLevel;
|
|
||||||
} else {
|
|
||||||
qDebug() << "登录失败!" << accessLevel;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
Binary file not shown.
@ -51,7 +51,7 @@ target_link_libraries(XNMonitorServer PRIVATE
|
|||||||
OpenSSL::Crypto
|
OpenSSL::Crypto
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_definitions(XNMonitorServer PRIVATE LOGIN_LIBRARY)
|
target_compile_definitions(XNMonitorServer PRIVATE XNMONITOR_SERVER_LIBRARY)
|
||||||
|
|
||||||
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||||
set(CMAKE_INSTALL_PREFIX "${XNCore_PATH}" CACHE PATH "Install path prefix" FORCE)
|
set(CMAKE_INSTALL_PREFIX "${XNCore_PATH}" CACHE PATH "Install path prefix" FORCE)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user