|
@@ -3,6 +3,8 @@ package org.snowchen.GreatSonServer.programinit;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.sql.*;
|
|
|
import java.util.Scanner;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
|
|
|
import static org.snowchen.GreatSonServer.programinit.EncryptionModule.hashPassword;
|
|
|
|
|
@@ -23,27 +25,38 @@ public class UserLogin {
|
|
|
System.out.println("用户名不存在,请重新输入:");
|
|
|
continue;
|
|
|
}
|
|
|
- break;
|
|
|
- }
|
|
|
|
|
|
- while (true) {
|
|
|
- System.out.println("请输入密码:");
|
|
|
- this.user_password = scan.nextLine();
|
|
|
- if (this.user_password.isEmpty()) {
|
|
|
- System.out.println("密码不能为空,请重新输入:");
|
|
|
- continue;
|
|
|
- }
|
|
|
- try {
|
|
|
- this.user_password = hashPassword(this.user_password);
|
|
|
- if (verifyPassword(this.user_name, this.user_password)) {
|
|
|
- System.out.println("登录成功!");
|
|
|
- break;
|
|
|
- } else {
|
|
|
- System.out.println("密码错误,请重新输入密码!");
|
|
|
+ // 检查用户上次登录时间
|
|
|
+ LocalDateTime lastLoginTime = getLastLoginTime(this.user_name);
|
|
|
+ LocalDateTime currentTime = LocalDateTime.now();
|
|
|
+ long minutesDifference = ChronoUnit.MINUTES.between(lastLoginTime, currentTime);
|
|
|
+
|
|
|
+ if (minutesDifference <= 10) {
|
|
|
+ System.out.println("已为您自动登录");
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ System.out.println("上次登录时间超过10分钟,请输入密码:");
|
|
|
+ while (true) {
|
|
|
+ System.out.println("请输入密码:");
|
|
|
+ this.user_password = scan.nextLine();
|
|
|
+ if (this.user_password.isEmpty()) {
|
|
|
+ System.out.println("密码不能为空,请重新输入:");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ this.user_password = hashPassword(this.user_password);
|
|
|
+ if (verifyPassword(this.user_name, this.user_password)) {
|
|
|
+ System.out.println("登录成功!");
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ System.out.println("密码错误,请重新输入密码!");
|
|
|
+ }
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ System.err.println("加密错误: " + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
- } catch (NoSuchAlgorithmException e) {
|
|
|
- System.err.println("加密错误: " + e.getMessage());
|
|
|
}
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -73,6 +86,32 @@ public class UserLogin {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取用户的上次登录时间
|
|
|
+ *
|
|
|
+ * @param username 用户名
|
|
|
+ * @return 用户的上次登录时间
|
|
|
+ */
|
|
|
+ private LocalDateTime getLastLoginTime(String username) {
|
|
|
+ String url = "jdbc:sqlite:identifier.sqlite";
|
|
|
+ String sql = "SELECT user_last_login_time FROM user_data WHERE user_name = ?";
|
|
|
+
|
|
|
+ try (Connection conn = DriverManager.getConnection(url);
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
|
+
|
|
|
+ pstmt.setString(1, username);
|
|
|
+ try (ResultSet rs = pstmt.executeQuery()) {
|
|
|
+ if (rs.next()) {
|
|
|
+ Timestamp timestamp = rs.getTimestamp("user_last_login_time");
|
|
|
+ return timestamp.toLocalDateTime();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.err.println("数据库错误: " + e.getMessage());
|
|
|
+ }
|
|
|
+ return LocalDateTime.now(); // 如果获取失败,默认为当前时间
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证密码是否正确
|
|
|
*
|
|
@@ -100,9 +139,9 @@ public class UserLogin {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public static void main(String[] args) {
|
|
|
new UserLogin();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|