From f5b1a9f63887a224931ce6d7783386db5de17353 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Mon, 19 Oct 2026 01:37:21 -0400 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E6=88=B7=E4=B9=9F=E8=A6=81=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E9=85=8D=E7=BD=AE=E6=89=80=E5=B1=9Eapps=E7=9A=84api?= =?UTF-8?q?=5Fsecret=E5=92=8Capi=5Fkey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AppApiKeysServiceImpl.java | 34 +++++++++---- sql/merchant_api_key_permissions.sql | 49 +++++++++++++++++++ 2 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 sql/merchant_api_key_permissions.sql diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java index 658a194..90203f4 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java @@ -58,31 +58,45 @@ public class AppApiKeysServiceImpl extends ServiceImpl list) { + Long currentUserId = requireCurrentUserId(); for (AppApiKeysPlus item : list) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null || !Objects.equals(item.getUserId(), currentUserId)) { + throw new CustomException("无权操作该 API Key"); } } } private void checkUser(List list) { + Long currentUserId = requireCurrentUserId(); for (AppApiKeys item : list) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null || !Objects.equals(item.getUserId(), currentUserId)) { + throw new CustomException("无权操作该 API Key"); } } } private void checkUser(AppApiKeysPlus item) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null) { + throw new CustomException("API Key 不存在或已删除"); + } + if (!Objects.equals(item.getUserId(), requireCurrentUserId())) { + throw new CustomException("无权操作该 API Key"); } } private void checkUser(AppApiKeys item) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null) { + throw new CustomException("API Key 不存在或已删除"); } + if (!Objects.equals(item.getUserId(), requireCurrentUserId())) { + throw new CustomException("无权操作该 API Key"); + } + } + + private Long requireCurrentUserId() { + Long userId = SecurityUtils.getUserId(); + if (userId == null) throw new CustomException("无法获取当前用户"); + return userId; } @Override @@ -297,7 +311,7 @@ public class AppApiKeysServiceImpl extends ServiceImpl