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