From 0826788fb874adcd3b2ac70aa90ffbf016280e38 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Thu, 28 Nov 2024 17:37:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/EntityNotFoundException.java | 19 +++++++ .../framework/security/SecurityUtils.java | 57 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 backend/src/main/java/com/qqchen/deploy/backend/framework/exception/EntityNotFoundException.java create mode 100644 backend/src/main/java/com/qqchen/deploy/backend/framework/security/SecurityUtils.java diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/EntityNotFoundException.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/EntityNotFoundException.java new file mode 100644 index 00000000..8ea360e2 --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/exception/EntityNotFoundException.java @@ -0,0 +1,19 @@ +package com.qqchen.deploy.backend.framework.exception; + +import com.qqchen.deploy.backend.framework.enums.ResponseCode; +import java.io.Serializable; + +public class EntityNotFoundException extends BusinessException { + + public EntityNotFoundException(Serializable id) { + super(ResponseCode.DATA_NOT_FOUND, new Object[]{id}); + } + + public EntityNotFoundException(String message) { + super(ResponseCode.DATA_NOT_FOUND, new Object[]{message}); + } + + public EntityNotFoundException(String entityName, Serializable id) { + super(ResponseCode.DATA_NOT_FOUND, new Object[]{entityName, id}); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/qqchen/deploy/backend/framework/security/SecurityUtils.java b/backend/src/main/java/com/qqchen/deploy/backend/framework/security/SecurityUtils.java new file mode 100644 index 00000000..ab157039 --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/framework/security/SecurityUtils.java @@ -0,0 +1,57 @@ +package com.qqchen.deploy.backend.framework.security; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; + +public class SecurityUtils { + + /** + * 获取当前登录用户名 + */ + public static String getCurrentUsername() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || !authentication.isAuthenticated()) { + return "SYSTEM"; + } + + Object principal = authentication.getPrincipal(); + if (principal instanceof UserDetails) { + return ((UserDetails) principal).getUsername(); + } + return principal.toString(); + } + + /** + * 检查是否有指定权限 + */ + public static boolean hasPermission(String permission) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + return authentication != null && + authentication.isAuthenticated() && + authentication.getAuthorities().stream() + .anyMatch(auth -> auth.getAuthority().equals(permission)); + } + + /** + * 获取当前用户ID + */ + public static Long getCurrentUserId() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null && authentication.isAuthenticated()) { + Object principal = authentication.getPrincipal(); + if (principal instanceof UserDetails) { + return Long.valueOf(((UserDetails) principal).getUsername()); + } + } + return null; + } + + /** + * 检查是否已认证 + */ + public static boolean isAuthenticated() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + return authentication != null && authentication.isAuthenticated(); + } +} \ No newline at end of file