From 2cf3533141262d32908166d2b41a858f51bd2d79 Mon Sep 17 00:00:00 2001
From: cat_shark <1716967236@qq.com>
Date: Thu, 18 Jun 2026 00:06:42 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E7=99=BB=E5=BD=95=E5=AF=86=E7=A0=81?=
=?UTF-8?q?=E6=94=B9=E7=94=A8=20BCrypt=20=E6=A0=A1=E9=AA=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 7 ++++++
.../service/impl/UserServiceImpl.java | 16 +++++++------
...20260617_1__alter_user_password_length.sql | 1 +
.../utils/BCryptPasswordGeneratorTest.java | 24 +++++++++++++++++++
4 files changed, 41 insertions(+), 7 deletions(-)
create mode 100644 src/main/resources/db/migration/V20260617_1__alter_user_password_length.sql
create mode 100644 src/test/java/com/guo/learningprogresstracker/utils/BCryptPasswordGeneratorTest.java
diff --git a/pom.xml b/pom.xml
index 605e56d..e1179eb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -129,6 +129,13 @@
spring-boot-starter-actuator
+
+
+ org.mindrot
+ jbcrypt
+ 0.4
+
+
diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/UserServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/UserServiceImpl.java
index 9baa745..0f2516b 100644
--- a/src/main/java/com/guo/learningprogresstracker/service/impl/UserServiceImpl.java
+++ b/src/main/java/com/guo/learningprogresstracker/service/impl/UserServiceImpl.java
@@ -7,6 +7,7 @@ import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.service.UserService;
import com.guo.learningprogresstracker.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
+import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;
/**
@@ -23,12 +24,13 @@ public class UserServiceImpl extends ServiceImpl
public String authenticate(String username, String password) throws AppException {
UserEntity userEntity = this.getOneOpt(Wrappers.lambdaQuery()
- .eq(UserEntity::getUserName, username)
- .eq(UserEntity::getUserPassword, password)).orElseThrow(() -> new AppException("账号或密码错误!"));
- return userEntity.getId();
+ .eq(UserEntity::getUserName, username))
+ .orElseThrow(() -> new AppException("账号或密码错误!"));
+
+ if (!BCrypt.checkpw(password, userEntity.getUserPassword())) {
+ throw new AppException("账号或密码错误!");
+ }
+
+ return userEntity.getId();
}
}
-
-
-
-
diff --git a/src/main/resources/db/migration/V20260617_1__alter_user_password_length.sql b/src/main/resources/db/migration/V20260617_1__alter_user_password_length.sql
new file mode 100644
index 0000000..ecc8282
--- /dev/null
+++ b/src/main/resources/db/migration/V20260617_1__alter_user_password_length.sql
@@ -0,0 +1 @@
+ALTER TABLE `user` MODIFY COLUMN `user_password` varchar(60) NOT NULL COMMENT '账号密码(BCrypt哈希)';
diff --git a/src/test/java/com/guo/learningprogresstracker/utils/BCryptPasswordGeneratorTest.java b/src/test/java/com/guo/learningprogresstracker/utils/BCryptPasswordGeneratorTest.java
new file mode 100644
index 0000000..3f2d37a
--- /dev/null
+++ b/src/test/java/com/guo/learningprogresstracker/utils/BCryptPasswordGeneratorTest.java
@@ -0,0 +1,24 @@
+package com.guo.learningprogresstracker.utils;
+
+import org.junit.jupiter.api.Test;
+import org.mindrot.jbcrypt.BCrypt;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class BCryptPasswordGeneratorTest {
+
+ @Test
+ void generatePasswordHash() {
+ String password = System.getProperty("password", "admin");
+ int rounds = Integer.parseInt(System.getProperty("rounds", "10"));
+
+ String hash = BCrypt.hashpw(password, BCrypt.gensalt(rounds));
+
+ System.out.println("BCrypt hash:");
+ System.out.println(hash);
+ System.out.println("SQL:");
+ System.out.printf("UPDATE `user` SET `user_password` = '%s' WHERE `user_name` = 'admin';%n", hash);
+
+ assertTrue(BCrypt.checkpw(password, hash));
+ }
+}