可正常启动
This commit is contained in:
parent
9089d9bd46
commit
0826788fb8
@ -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});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user