Refactor: Aspect 최적화

This commit is contained in:
김용수 2024-09-23 22:24:18 +09:00
parent 211d77481d
commit e2fd9ace95

View File

@ -30,27 +30,34 @@ public class PrivilegeCheckAspect {
// CheckPrivilege 어노테이션이 붙은 메서드가 실행되기전 실행 // CheckPrivilege 어노테이션이 붙은 메서드가 실행되기전 실행
@Before("@annotation(checkPrivilege)") @Before("@annotation(checkPrivilege)")
public void checkPrivilege(JoinPoint joinPoint, CheckPrivilege checkPrivilege) { public void checkPrivilege(JoinPoint joinPoint, CheckPrivilege checkPrivilege) {
int memberId = getMemberId();
int projectId = getProjectId(joinPoint);
checkPrivilegeUnauthorized(memberId, projectId, checkPrivilege.value());
}
private int getProjectId(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod(); Method method = signature.getMethod();
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
Parameter[] parameters = method.getParameters(); Parameter[] parameters = method.getParameters();
Object principal = SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
int memberId = ((AuthMemberDto) principal).getId();
Integer projectId = null;
for (int paramIdx = 0; paramIdx < parameters.length; paramIdx++) { for (int paramIdx = 0; paramIdx < parameters.length; paramIdx++) {
String paramName = parameters[paramIdx].getName(); String paramName = parameters[paramIdx].getName();
if (paramName.equals("projectId")) { if (paramName.equals("projectId")) {
projectId = (Integer) args[paramIdx]; return (Integer) args[paramIdx];
break;
} }
} }
checkPrivilegeUnauthorized(memberId, projectId, checkPrivilege.value()); throw new CustomException(ErrorCode.SERVER_ERROR);
}
private int getMemberId() {
Object principal = SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
return ((AuthMemberDto) principal).getId();
} }
public void checkPrivilegeUnauthorized(final Integer memberId, final Integer projectId, final PrivilegeType privilegeType) { public void checkPrivilegeUnauthorized(final Integer memberId, final Integer projectId, final PrivilegeType privilegeType) {