修复: 密码验证统一使用checkPasswordWithFallback,消除重复checkPassword定义;LoginManager/main/web使用新方法

This commit is contained in:
2026-06-28 23:12:20 +08:00
parent f3362380a3
commit a33757a465
4 changed files with 65 additions and 101 deletions
@@ -90,9 +90,8 @@ public class ChatInputManager {
name, "password_salt"); name, "password_salt");
String hash = String hash =
PasswordUtils.hash(msg, salt); PasswordUtils.hash(msg, salt);
boolean mainOk = String result = db.checkPasswordWithFallback(name, hash);
db.checkPassword(name, hash); if ("main".equals(result)) {
if (mainOk) {
state.type = state.type =
InputType.CHANGE_PWD_STEP2; InputType.CHANGE_PWD_STEP2;
state.ticketTitle = "main"; state.ticketTitle = "main";
@@ -100,10 +99,7 @@ public class ChatInputManager {
"§e请输入新密码:"); "§e请输入新密码:");
return; return;
} }
boolean tempOk = if ("temp".equals(result)) {
db.checkPasswordOrTemp(
name, hash);
if (tempOk) {
state.type = state.type =
InputType.CHANGE_PWD_STEP2; InputType.CHANGE_PWD_STEP2;
state.ticketTitle = "temp"; state.ticketTitle = "temp";
+35 -56
View File
@@ -482,9 +482,11 @@ public class DatabaseManager {
? String.valueOf(ip) : null; ? String.valueOf(ip) : null;
} }
public boolean checkPasswordOrTemp( /**
String name, String hash) { * 验证密码(仅主密码)
// 先检查主密码 * @return true if matched
*/
public boolean checkPassword(String name, String hash) {
try { try {
PreparedStatement ps = db.prepareStatement( PreparedStatement ps = db.prepareStatement(
"SELECT password_hash " "SELECT password_hash "
@@ -497,8 +499,7 @@ public class DatabaseManager {
rs.getString("password_hash"); rs.getString("password_hash");
rs.close(); rs.close();
ps.close(); ps.close();
if (hash.equals(mainHash)) return hash.equals(mainHash);
return true;
} else { } else {
rs.close(); rs.close();
ps.close(); ps.close();
@@ -506,47 +507,51 @@ public class DatabaseManager {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 再检查临时密码 return false;
}
/**
* 验证密码(主密码优先,不通过再查临时密码)
* 注意: 传入的 hash 必须是主密码的 hash
* @return "main" | "temp" | null
*/
public String checkPasswordWithFallback(String name, String mainHash) {
// 先检查主密码
if (checkPassword(name, mainHash)) {
return "main";
}
// 主密码不匹配,检查临时密码
try { try {
PreparedStatement ps2 = db.prepareStatement( PreparedStatement ps = db.prepareStatement(
"SELECT temp_password, " "SELECT temp_password, "
+ "temp_pw_expire, " + "temp_pw_expire, "
+ "temp_pw_used " + "temp_pw_used "
+ "FROM users " + "FROM users "
+ "WHERE player_name=?"); + "WHERE player_name=?");
ps2.setString(1, name); ps.setString(1, name);
ResultSet rs2 = ps2.executeQuery(); ResultSet rs = ps.executeQuery();
if (rs2.next()) { if (rs.next()) {
String tempHash = String tempHash = rs.getString("temp_password");
rs2.getString( long expire = rs.getLong("temp_pw_expire");
"temp_password"); int used = rs.getInt("temp_pw_used");
long expire = rs.close();
rs2.getLong( ps.close();
"temp_pw_expire");
int used =
rs2.getInt(
"temp_pw_used");
rs2.close();
ps2.close();
if (tempHash != null if (tempHash != null
&& !tempHash.isEmpty() && !tempHash.isEmpty()
&& hash.equals(tempHash) && mainHash.equals(tempHash)
&& System.currentTimeMillis() && System.currentTimeMillis() < expire
< expire
&& used != 1) { && used != 1) {
return true; return "temp";
} }
} else { } else {
rs2.close(); rs.close();
ps2.close(); ps.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return false; return null;
} }
public Map<String, Object> getUser(String name) { public Map<String, Object> getUser(String name) {
Map<String, Object> r = Map<String, Object> r =
new LinkedHashMap<>(); new LinkedHashMap<>();
@@ -613,32 +618,6 @@ public class DatabaseManager {
} }
} }
public boolean checkPassword(String name,
String hash) {
try {
PreparedStatement ps = db.prepareStatement(
"SELECT password_hash "
+ "FROM users "
+ "WHERE player_name=?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String stored =
rs.getString("password_hash");
rs.close();
ps.close();
return hash.equals(stored);
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean isUsingTempPassword(String name) { public boolean isUsingTempPassword(String name) {
String h = (String) getField(name, String h = (String) getField(name,
"temp_password"); "temp_password");
+3 -3
View File
@@ -209,11 +209,11 @@ public class LoginManager {
return true; return true;
} }
boolean matchTemp = String pwdResult =
plugin.getDb() plugin.getDb()
.checkPasswordOrTemp( .checkPasswordWithFallback(
name, hash); name, hash);
if (matchTemp) { if (pwdResult != null) {
plugin.getLoggedIn().add(name); plugin.getLoggedIn().add(name);
// ★ 记录Java手动登录:5分钟内重连可直接放行(检查点1) // ★ 记录Java手动登录:5分钟内重连可直接放行(检查点1)
if (plugin.webManager != null) { if (plugin.webManager != null) {
+24 -35
View File
@@ -1202,13 +1202,13 @@ public class Main extends JavaPlugin
String hash = PasswordUtils.hash(password, salt); String hash = PasswordUtils.hash(password, salt);
getLogger().info("[Web密码验证] 计算hash=" + hash + " 存储hash=" + storedHash + " 是否匹配=" + hash.equals(storedHash)); getLogger().info("[Web密码验证] 计算hash=" + hash + " 存储hash=" + storedHash + " 是否匹配=" + hash.equals(storedHash));
// ★ 与 /login 保持一致:先查主密码,再查临时密码 // ★ 与 /login 保持一致:先查主密码,再查临时密码
if (db.checkPassword(playerName, hash)) { String pwdResult = db.checkPasswordWithFallback(playerName, hash);
getLogger().info("[Web密码验证] ★ 主密码验证成功"); if (pwdResult != null) {
return "\"success\""; if ("main".equals(pwdResult)) {
} getLogger().info("[Web密码验证] ★ 主密码验证成功");
getLogger().info("[Web密码验证] 主密码不匹配,检查临时密码..."); } else {
if (db.checkPasswordOrTemp(playerName, hash)) { getLogger().info("[Web密码验证] ★ 临时密码验证成功");
getLogger().info("[Web密码验证] ★ 临时密码验证成功"); }
return "\"success\""; return "\"success\"";
} }
getLogger().info("[Web密码验证] 临时密码也不匹配,密码验证失败"); getLogger().info("[Web密码验证] 临时密码也不匹配,密码验证失败");
@@ -5080,14 +5080,13 @@ public class Main extends JavaPlugin
String salt = (String) db.getField( String salt = (String) db.getField(
p2.getName(), "password_salt"); p2.getName(), "password_salt");
String hash = PasswordUtils.hash(oldPwd, salt); String hash = PasswordUtils.hash(oldPwd, salt);
boolean mainOk = db.checkPassword( String pwdResult = db.checkPasswordWithFallback(
p2.getName(), hash); p2.getName(), hash);
boolean tempOk = db.checkPasswordOrTemp( if (pwdResult == null) {
p2.getName(), hash);
if (!mainOk && !tempOk) {
p2.sendMessage(config.msg("password_wrong")); p2.sendMessage(config.msg("password_wrong"));
return true; return true;
} }
boolean isTemp = "temp".equals(pwdResult);
String oldHash = (String) db.getField( String oldHash = (String) db.getField(
p2.getName(), "password_hash"); p2.getName(), "password_hash");
String newSalt = PasswordUtils.generateSalt(); String newSalt = PasswordUtils.generateSalt();
@@ -5104,13 +5103,9 @@ public class Main extends JavaPlugin
Bukkit.getScheduler().runTaskAsynchronously(this, () -> { Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
webManager.pushWebLoginCredentials(); webManager.pushWebLoginCredentials();
}); });
if (!mainOk && tempOk) if (isTemp)
db.clearTempPassword(p2.getName()); db.clearTempPassword(p2.getName());
// pw 命令改密码成功后
needsPasswordChange.remove(p2.getName()); needsPasswordChange.remove(p2.getName());
// 清除临时密码
if (!mainOk && tempOk)
db.clearTempPassword(p2.getName());
return true; return true;
} }
@@ -5120,18 +5115,16 @@ public class Main extends JavaPlugin
String salt = (String) db.getField( String salt = (String) db.getField(
p2.getName(), "password_salt"); p2.getName(), "password_salt");
String hash = PasswordUtils.hash(oldPwd, salt); String hash = PasswordUtils.hash(oldPwd, salt);
boolean mainOk = db.checkPassword( String pwdResult = db.checkPasswordWithFallback(
p2.getName(), hash); p2.getName(), hash);
boolean tempOk = db.checkPasswordOrTemp( if (pwdResult == null) {
p2.getName(), hash);
if (!mainOk && !tempOk) {
p2.sendMessage(config.msg("password_wrong")); p2.sendMessage(config.msg("password_wrong"));
return true; return true;
} }
chatInput.getState(p2).type = chatInput.getState(p2).type =
ChatInputManager.InputType.CHANGE_PWD_STEP2; ChatInputManager.InputType.CHANGE_PWD_STEP2;
chatInput.getState(p2).ticketTitle = chatInput.getState(p2).ticketTitle =
mainOk ? "main" : "temp"; pwdResult;
p2.sendMessage("§e请输入新密码:"); p2.sendMessage("§e请输入新密码:");
return true; return true;
} }
@@ -5353,17 +5346,15 @@ public class Main extends JavaPlugin
String hash = String hash =
PasswordUtils.hash( PasswordUtils.hash(
oldPwd, salt); oldPwd, salt);
boolean mainOk = String pwdResult =
db.checkPassword( db.checkPasswordWithFallback(
p2.getName(), hash); p2.getName(), hash);
boolean tempOk = if (pwdResult == null) {
db.checkPasswordOrTemp(
p2.getName(), hash);
if (!mainOk && !tempOk) {
p2.sendMessage(config.msg( p2.sendMessage(config.msg(
"password_wrong")); "password_wrong"));
return true; return true;
} }
boolean isTemp = "temp".equals(pwdResult);
String oldHash = String oldHash =
(String) db.getField( (String) db.getField(
p2.getName(), p2.getName(),
@@ -5390,7 +5381,7 @@ public class Main extends JavaPlugin
Bukkit.getScheduler().runTaskAsynchronously(this, () -> { Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
self2.webManager.pushWebLoginCredentials(); self2.webManager.pushWebLoginCredentials();
}); });
if (!mainOk && tempOk) { if (!isTemp) {
db.clearTempPassword( db.clearTempPassword(
p2.getName()); p2.getName());
} }
@@ -5411,23 +5402,21 @@ public class Main extends JavaPlugin
String hash = String hash =
PasswordUtils.hash( PasswordUtils.hash(
oldPwd, salt); oldPwd, salt);
boolean mainOk = String pwdResult =
db.checkPassword( db.checkPasswordWithFallback(
p2.getName(), hash); p2.getName(), hash);
boolean tempOk = if (pwdResult == null) {
db.checkPasswordOrTemp(
p2.getName(), hash);
if (!mainOk && !tempOk) {
p2.sendMessage(config.msg( p2.sendMessage(config.msg(
"password_wrong")); "password_wrong"));
return true; return true;
} }
boolean isTemp = "temp".equals(pwdResult);
chatInput.getState(p2).type = chatInput.getState(p2).type =
ChatInputManager.InputType ChatInputManager.InputType
.CHANGE_PWD_STEP2; .CHANGE_PWD_STEP2;
chatInput.getState(p2) chatInput.getState(p2)
.ticketTitle = .ticketTitle =
mainOk ? "main" : "temp"; pwdResult;
p2.sendMessage( p2.sendMessage(
"§e请输入新密码:"); "§e请输入新密码:");
return true; return true;