From bf1f98c5b741f6b523194c427cc07b484088e150 Mon Sep 17 00:00:00 2001
From: xiaodai <1318762926@qq.com>
Date: Thu, 14 Aug 2025 12:04:12 +0800
Subject: [PATCH 1/6] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=A2=9E=E5=8A=A0?=
=?UTF-8?q?=E5=AF=B9=E5=AF=86=E7=A0=81rsa=E5=AF=86=E6=96=87=E4=BC=A0?=
=?UTF-8?q?=E8=BE=93?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../app/admin/controller/LoginController.php | 19 +++++++++---
.../services/system/SysAdminService.php | 29 ++++++++++++++++++-
2 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/server/app/admin/controller/LoginController.php b/server/app/admin/controller/LoginController.php
index 25fcf34..68f77af 100644
--- a/server/app/admin/controller/LoginController.php
+++ b/server/app/admin/controller/LoginController.php
@@ -13,6 +13,7 @@
namespace app\admin\controller;
use app\common\services\system\SysAdminService;
+use core\cache\CacheService;
use core\exception\handler\AdminException;
use core\jwt\JwtToken;
use core\utils\Json;
@@ -55,7 +56,18 @@ class LoginController extends Crud
*/
public function getCaptchaOpenFlag(Request $request): \support\Response
{
- return Json::success('ok', ['flag' => config('core.captcha.app.enable', false)]);
+ try {
+ // 生成密钥对
+ $cache = Container::make(CacheService::class,[]);
+ // 使用 uniqid 增加唯一性
+ $keyId = bin2hex(random_bytes(8)) . uniqid();
+ $keys = $this->service->generateRSAKeys();
+ // 存储私钥到缓存,用于解密密码
+ $cache->set("rsa_private_key:$keyId", $keys['private'], 60); // 5分钟过期
+ return Json::success('ok', ['flag' => config('core.captcha.app.enable', false),'key_id'=>$keyId,'public_key'=>$keys['public']]);
+ } catch (\Throwable $e) {
+ return Json::fail($e->getMessage());
+ }
}
/**
@@ -113,9 +125,8 @@ class LoginController extends Crud
$code = $request->input('code', '');
$uuid = $request->input('uuid', '');
$type = $request->input('type', 'admin');
- $tenantId = $request->input('tenant_id', '');
$grantType = $request->input('grant_type', 'default');//refresh_token sms default 可以自行定义拓展登录方式
-
+ $keyId = $request->input('key_id', '');//获取公钥Id
$service = Container::make(SysAdminService::class);
$captcha = new Captcha();
@@ -139,7 +150,7 @@ class LoginController extends Crud
}
$username = $info->getData('user_name');
}
- $data = $service->login($username, $password, $type, $grantType, $tenantId);
+ $data = $service->login($username, $password, $type, $grantType, ['keyId'=> $keyId ?? '']);
return Json::success('ok', $data);
} catch (\Throwable $e) {
return Json::fail($e->getMessage());
diff --git a/server/app/common/services/system/SysAdminService.php b/server/app/common/services/system/SysAdminService.php
index e5f3bff..390fa55 100644
--- a/server/app/common/services/system/SysAdminService.php
+++ b/server/app/common/services/system/SysAdminService.php
@@ -15,6 +15,7 @@ namespace app\common\services\system;
use app\common\dao\system\SysAdminDao;
use app\common\model\system\SysAdmin;
use core\abstract\BaseService;
+use core\cache\CacheService;
use core\casbin\Permission;
use core\enum\system\PolicyPrefix;
use core\exception\handler\AdminException;
@@ -244,10 +245,12 @@ class SysAdminService extends BaseService
* @return array
* @throws \Exception
*/
- public function login(string $username, string $password = '', string $type = 'admin', string $grantType = 'default', string|int $tenantId = ''): array
+ public function login(string $username, string $password = '', string $type = 'admin', string $grantType = 'default', array $params = []): array
{
$adminInfo = $this->getAdminByName($username);
$this->validateAdminStatus($adminInfo);
+ $decryptedPassword = $this->validateRsaKeys($params['keyId'], $password);
+ $this->validatePassword($adminInfo, $decryptedPassword, $grantType);
$this->validatePassword($adminInfo, $password, $grantType);
[$userInfo, $token] = $this->generateTokenData($adminInfo, $type);
$this->emitLoginSuccessEvent(array_merge($userInfo, $token), $tenant?->id ?? null);
@@ -481,4 +484,28 @@ class SysAdminService extends BaseService
preg_match($pattern, $url, $matches);
return $matches[1] ?? '';
}
+ /**
+ * 校验密钥
+ * @param $keyId
+ * @param $encryptedPassword
+ * @return string
+ * @throws AdminException
+ */
+ private function validateRsaKeys($keyId, $encryptedPassword): string
+ {
+ $cache = Container::make(CacheService::class,[]);
+ $privateKey = $cache->get("rsa_private_key:$keyId");
+ if (!$privateKey) {
+ throw new AdminException('私钥不存在或已过期,请刷新页面重试');
+ }
+ $privateKeyResource = openssl_pkey_get_private($privateKey);
+ $decrypted = '';
+ $encryptedData = base64_decode($encryptedPassword);
+ if (openssl_private_decrypt($encryptedData, $decrypted, $privateKeyResource)) {
+ $cache->delete("rsa_private_key:$keyId"); // 删除私钥,防止泄露
+ return $decrypted;
+ } else {
+ throw new AdminException('解密失败!');
+ }
+ }
}
--
Gitee
From f4736d33a00082c53ea1eeec7d2dfcf34cd8c4fa Mon Sep 17 00:00:00 2001
From: xiaodai <1318762926@qq.com>
Date: Thu, 14 Aug 2025 14:25:58 +0800
Subject: [PATCH 2/6] =?UTF-8?q?=E5=AF=86=E7=A0=81rsa=E5=8A=A0=E5=AF=86?=
=?UTF-8?q?=E4=BC=A0=E8=BE=93?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
web/src/views/core/authentication/login.vue | 105 +++++++++++---------
1 file changed, 57 insertions(+), 48 deletions(-)
diff --git a/web/src/views/core/authentication/login.vue b/web/src/views/core/authentication/login.vue
index 2754f86..314500a 100644
--- a/web/src/views/core/authentication/login.vue
+++ b/web/src/views/core/authentication/login.vue
@@ -1,6 +1,5 @@
--
Gitee
From 39af890d0ae577e3dc4451096e05394587839ca0 Mon Sep 17 00:00:00 2001
From: xiaodai <1318762926@qq.com>
Date: Thu, 14 Aug 2025 15:36:39 +0800
Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=AA=8C=E8=AF=81?=
=?UTF-8?q?=E7=A0=81=E6=9C=AA=E5=BC=80=E5=90=AF=E4=B9=9F=E8=B0=83=E7=94=A8?=
=?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
web/src/views/core/authentication/login.vue | 117 ++++++++++----------
1 file changed, 61 insertions(+), 56 deletions(-)
diff --git a/web/src/views/core/authentication/login.vue b/web/src/views/core/authentication/login.vue
index 314500a..1013e7b 100644
--- a/web/src/views/core/authentication/login.vue
+++ b/web/src/views/core/authentication/login.vue
@@ -1,7 +1,7 @@
@@ -139,7 +144,7 @@ const handleSubmit = async (data: any) => {
{
:show-third-party-login="false"
@submit="handleSubmit"
/>
-
+
\ No newline at end of file
--
Gitee
From 29e2ca9014653c6e478724df8f2df8b3055c754e Mon Sep 17 00:00:00 2001
From: xiaodai <1318762926@qq.com>
Date: Thu, 14 Aug 2025 17:40:16 +0800
Subject: [PATCH 4/6] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=8F=90=E4=BA=A4?=
=?UTF-8?q?=E5=AF=86=E7=A0=81=E8=BF=9B=E8=A1=8Crsa=E5=8A=A0=E5=AF=86?=
=?UTF-8?q?=E6=8F=90=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
web/src/views/core/authentication/login.vue | 166 ++++++++++----------
1 file changed, 79 insertions(+), 87 deletions(-)
diff --git a/web/src/views/core/authentication/login.vue b/web/src/views/core/authentication/login.vue
index 1013e7b..a04a414 100644
--- a/web/src/views/core/authentication/login.vue
+++ b/web/src/views/core/authentication/login.vue
@@ -1,13 +1,14 @@