Преглед на файлове

增加了用户注册密码强度要求,规定了用户注册格式

Snow преди 2 седмици
родител
ревизия
5e98cdfa46

+ 9 - 0
pom.xml

@@ -14,4 +14,13 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.xerial</groupId>
+            <artifactId>sqlite-jdbc</artifactId>
+            <version>3.41.2.2</version>
+            <scope>runtime</scope>
+        </dependency>
+    </dependencies>
+
 </project>

+ 4 - 0
src/main/java/org/snowchen/GreatSonServer/Main.java

@@ -2,10 +2,14 @@ package org.snowchen.GreatSonServer;
 
 import org.snowchen.GreatSonServer.programinit.UserInit;
 
+import java.util.Arrays;
+
 public class Main {
     public static void main(String[] args) {
 
         System.out.printf("Hello and welcome!");
         UserInit userInit = new UserInit();
+        String[] userData = userInit.getUserData(); // 外部获取UserData数组
+        System.out.println("UserData: " + Arrays.toString(userData));
     }
 }

+ 0 - 8
src/main/java/org/snowchen/GreatSonServer/databasecore/SQLiteConnect.java

@@ -1,8 +0,0 @@
-package org.snowchen.GreatSonServer.databasecore;
-
-
-public class SQLiteConnect {
-
-
-}
-

+ 52 - 0
src/main/java/org/snowchen/GreatSonServer/databasecore/SQLiteInit.java

@@ -0,0 +1,52 @@
+package org.snowchen.GreatSonServer.databasecore;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class SQLiteInit {
+
+    private static final String JDBC_DRIVER = "org.sqlite.JDBC";
+    private static final String DB_URL = "jdbc:sqlite:test.db";
+    //初始化数据库
+    public static void main(String[] args) {
+        try (Connection c = DriverManager.getConnection(DB_URL)) {
+            // 加载驱动程序
+            Class.forName(JDBC_DRIVER);
+
+            // 创建main表
+            String sqlMainTable = "CREATE TABLE IF NOT EXISTS main (" +
+                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
+                    "data TEXT" +
+                    ")";
+            try (Statement statement = c.createStatement()) {
+                statement.execute(sqlMainTable);
+                System.out.println("main表创建成功");
+            }
+
+            // 创建user_data表
+            String sqlUserDataTable = "CREATE TABLE IF NOT EXISTS user_data (" +
+                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
+                    "user_name TEXT NOT NULL, " +
+                    "user_password TEXT NOT NULL, " +
+                    "user_uuid TEXT NOT NULL, " +
+                    "user_permission_level INTEGER NOT NULL" + // 修改为 INTEGER
+                    ")";
+            try (Statement statement = c.createStatement()) {
+                statement.execute(sqlUserDataTable);
+                System.out.println("user_data表创建成功");
+            }
+
+        } catch (ClassNotFoundException e) {
+            System.err.println("SQLite JDBC driver not found.");
+            e.printStackTrace();
+        } catch (SQLException e) {
+            System.err.println("Database error: " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+}
+
+
+

+ 28 - 17
src/main/java/org/snowchen/GreatSonServer/programinit/UserInit.java

@@ -5,15 +5,17 @@ import java.util.Scanner;
 import java.util.UUID;
 
 public class UserInit {
-    Scanner scan = new Scanner(System.in);
-    String user_name;
-    String password;
-    String uuid;
-    String [] UserData;
+    private Scanner scan = new Scanner(System.in);
+    private String user_name;
+    private String user_password;
+    private String uuid;
+    private String user_register_time;
+    private int user_permission_level;
+    private String[] UserData; // 将UserData数组定义为私有
 
     public UserInit() {
-        UserData = new String[3];
-        //用户信息初始化
+        UserData = new String[5];
+        // 用户信息初始化
         System.out.println("请输入用户名:");
         this.user_name = scan.nextLine();
         while (this.user_name.isEmpty()) {
@@ -22,21 +24,28 @@ public class UserInit {
         }
         UserData[0] = this.user_name;
         System.out.println("请输入密码:");
-        this.password = scan.nextLine();
-        while (!isStrongPassword(this.password)) {
-            System.out.println("密码强度不够,请重新输入:");
-            this.password = scan.nextLine();
+        this.user_password = scan.nextLine();
+        while (!isStrongPassword(this.user_password)) {
+            System.out.println("密码需为8位以上大小写字母和数字组合,请重新输入:");
+            this.user_password = scan.nextLine();
         }
-        UserData[1] = this.password;
+        UserData[1] = this.user_password;
         this.uuid = UUID.randomUUID().toString(); // 使用标准的 UUID 生成方法
         UserData[2] = this.uuid;
-        System.out.println("用户名:" + this.user_name + "\n密码:" + this.password + "\nuuid:" + this.uuid);
-        System.out.println(Arrays.toString(UserData));
-
+        this.user_permission_level = 0;
+        UserData[3] = String.valueOf(this.user_permission_level);
+        this.user_register_time = String.valueOf(System.nanoTime());
+        UserData[4] = this.user_register_time;
+        System.out.println("用户名:" + this.user_name + "\n密码:" + this.user_password + "\nuuid:" + this.uuid + "\n注册时间:" + this.user_register_time);
+        System.out.println("UserData: " + Arrays.toString(UserData));
+    }
 
+    // 提供一个公共方法来获取UserData数组
+    public String[] getUserData() {
+        return UserData;
     }
 
-    private static final String PWD_REGEX = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()=_+;':,.?]).{8,}$";
+    private static final String PWD_REGEX = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$";
 
     /**
      * 检测密码是否为强密码
@@ -46,11 +55,13 @@ public class UserInit {
      * @return 如果密码符合强密码的要求且不为空字符串,则返回true;否则返回false
      */
     public static boolean isStrongPassword(String password) {
-        //密码强度检测
+        // 密码强度检测
         if (password == null || password.trim().isEmpty()) {
             return false;
         }
         return password.matches(PWD_REGEX);
     }
+
 }
 
+