diff --git a/pom.xml b/pom.xml index a0c33b8..8fdb633 100644 --- a/pom.xml +++ b/pom.xml @@ -9,49 +9,76 @@ 1.0.0 + 17 17 UTF-8 + + + 21.0.8 + com.google.code.gson gson 2.10.1 + + + + org.openjfx + javafx-controls + ${javafx.version} + + + + + org.openjfx + javafx-fxml + ${javafx.version} + + + + + org.openjfx + javafx-graphics + ${javafx.version} + - + org.apache.maven.plugins - maven-jar-plugin - 3.3.0 - - - default-jar - none - - + maven-compiler-plugin + 3.11.0 + + 17 + 17 + - + + maven-assembly-plugin 3.6.0 + com.mathgenerator.Application jar-with-dependencies - mathgenerator - false + mathgenerator + false + single @@ -65,5 +92,4 @@ - \ No newline at end of file diff --git a/src/main/java/com/mathgenerator/service/UserService.java b/src/main/java/com/mathgenerator/service/UserService.java index 05b2409..f154390 100644 --- a/src/main/java/com/mathgenerator/service/UserService.java +++ b/src/main/java/com/mathgenerator/service/UserService.java @@ -1,6 +1,7 @@ package com.mathgenerator.service; import com.google.gson.Gson; +import java.util.Objects; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.mathgenerator.model.User; @@ -81,10 +82,22 @@ public class UserService { * @return 成功返回true, 否则返回false */ public boolean register(String username, String email, String password) { - if (userDatabase.containsKey(username) || - userDatabase.values().stream().anyMatch(u -> u.email().equals(email))) { + // 1. 基础校验:防止 null 或空白输入 + if (username == null || email == null || password == null || + username.trim().isEmpty() || email.trim().isEmpty() || password.trim().isEmpty()) { + return false; + } + + // 2. 检查用户名或邮箱是否已存在(使用 Objects.equals 安全比较) + boolean usernameExists = userDatabase.containsKey(username); + boolean emailExists = userDatabase.values().stream() + .anyMatch(u -> Objects.equals(u.email(), email)); + + if (usernameExists || emailExists) { return false; // 用户名或邮箱已存在 } + + // 3. 创建新用户并保存 User newUser = new User(username, email, password); userDatabase.put(username, newUser); saveUsers();