Skip to content

Commit 94aca73

Browse files
feat(whitelist): 记录未授权访问日志到数据库
当玩家未通过白名单验证时,异步将访问日志写入数据库的 operation_log 表中, 包括玩家名称、UUID、IP 地址等信息,并扩展了操作类型约束以支持 'UNAUTHORIZED_ACCESS' 类型。 同时更新数据库表结构,增加对新操作类型的校验支持,并忽略 .VSCodeCounter 目录的版本控制。
1 parent e399423 commit 94aca73

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ usercache.json
6161
usernamecache.json
6262

6363
# Keep the built plugin JAR for distribution
64-
!target/convenient-access-*.jar
64+
!target/convenient-access-*.jar
65+
66+
/.VSCodeCounter

src/main/java/com/xaoxiao/convenientaccess/listener/WhitelistListener.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,41 @@ private String getCustomKickMessage(String playerName) {
213213
private void logUnauthorizedAccess(String playerName, String playerUuid, String ipAddress) {
214214
logger.warn("未授权访问尝试 - 玩家: {} ({}), IP: {}", playerName, playerUuid, ipAddress);
215215

216-
// TODO: 可以扩展为写入数据库操作日志
217-
// plugin.getDatabaseManager().executeAsync(connection -> {
218-
// // 插入操作日志
219-
// return null;
220-
// });
216+
// 异步写入数据库操作日志
217+
plugin.getWhitelistSystem().getDatabaseManager().executeAsync(connection -> {
218+
try {
219+
String sql = """
220+
INSERT INTO operation_log
221+
(operation_type, target_uuid, target_name, operator_ip, operator_agent,
222+
request_data, response_status, execution_time)
223+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
224+
""";
225+
226+
try (var pstmt = connection.prepareStatement(sql)) {
227+
pstmt.setString(1, "UNAUTHORIZED_ACCESS");
228+
pstmt.setString(2, playerUuid);
229+
pstmt.setString(3, playerName);
230+
pstmt.setString(4, ipAddress);
231+
pstmt.setString(5, "Minecraft Client"); // 游戏客户端
232+
pstmt.setString(6, String.format("{\"reason\":\"not_in_whitelist\",\"player\":\"%s\",\"uuid\":\"%s\"}",
233+
playerName, playerUuid));
234+
pstmt.setInt(7, 403); // HTTP 403 Forbidden 表示拒绝访问
235+
pstmt.setLong(8, 0); // 不需要记录执行时间
236+
237+
int affected = pstmt.executeUpdate();
238+
if (affected > 0) {
239+
logger.info("✅ 已记录未授权访问日志: {} ({})", playerName, ipAddress);
240+
} else {
241+
logger.warn("❌ 记录未授权访问日志失败: {}", playerName);
242+
}
243+
}
244+
245+
return null;
246+
} catch (java.sql.SQLException e) {
247+
logger.error("记录未授权访问日志时发生SQL异常: {} ({})", playerName, ipAddress, e);
248+
return null;
249+
}
250+
});
221251
}
222252

223253
/**

src/main/resources/schema/operation_log.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ CREATE TABLE IF NOT EXISTS operation_log (
1212
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1313

1414
-- 约束
15-
CONSTRAINT chk_operation_type CHECK (operation_type IN ('ADD', 'REMOVE', 'QUERY', 'BATCH_ADD', 'BATCH_REMOVE', 'SYNC'))
15+
CONSTRAINT chk_operation_type CHECK (operation_type IN ('ADD', 'REMOVE', 'QUERY', 'BATCH_ADD', 'BATCH_REMOVE', 'SYNC', 'UNAUTHORIZED_ACCESS'))
1616
);

0 commit comments

Comments
 (0)