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