可正常启动

This commit is contained in:
dengqichen 2024-11-28 17:37:40 +08:00
parent 9089d9bd46
commit 0826788fb8
2 changed files with 76 additions and 0 deletions

View File

@ -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});
}
}

View File

@ -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();
}
}