diff --git a/animory/src/main/java/com/daggle/animory/common/config/WebConfiguration.java b/animory/src/main/java/com/daggle/animory/common/config/WebConfiguration.java index fdd2f3a9..d6dd5b50 100644 --- a/animory/src/main/java/com/daggle/animory/common/config/WebConfiguration.java +++ b/animory/src/main/java/com/daggle/animory/common/config/WebConfiguration.java @@ -1,20 +1,23 @@ package com.daggle.animory.common.config; import com.daggle.animory.common.logger.RequestLogger; +import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration +@RequiredArgsConstructor public class WebConfiguration implements WebMvcConfigurer { + private final RequestLogger requestLogger; @Override public void addInterceptors(final InterceptorRegistry registry) { // Request Logger 를 모든 요청에 대해 적용 - registry.addInterceptor(new RequestLogger()).order(-1); + registry.addInterceptor(requestLogger); } @Override diff --git a/animory/src/main/java/com/daggle/animory/common/logger/RequestLogger.java b/animory/src/main/java/com/daggle/animory/common/logger/RequestLogger.java index 3d3055ab..ad5cf706 100644 --- a/animory/src/main/java/com/daggle/animory/common/logger/RequestLogger.java +++ b/animory/src/main/java/com/daggle/animory/common/logger/RequestLogger.java @@ -4,6 +4,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.MDC; import org.springframework.lang.NonNull; +import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; @@ -15,6 +16,7 @@ * HTTP Request의 Method, URI, Query Parameter를 로깅합니다.* */ @Slf4j +@Component public class RequestLogger implements HandlerInterceptor { @Override diff --git a/animory/src/main/java/com/daggle/animory/common/security/Authorized.java b/animory/src/main/java/com/daggle/animory/common/security/RequireRole.java similarity index 91% rename from animory/src/main/java/com/daggle/animory/common/security/Authorized.java rename to animory/src/main/java/com/daggle/animory/common/security/RequireRole.java index ea0fd223..1dddfd20 100644 --- a/animory/src/main/java/com/daggle/animory/common/security/Authorized.java +++ b/animory/src/main/java/com/daggle/animory/common/security/RequireRole.java @@ -11,7 +11,7 @@ /** *
- * Authorized가 붙은 Controller의 메소드는 인증된 사용자만 접근할 수 있습니다. + * Annotation이 붙은 Controller의 메소드는 인증된 사용자만 접근할 수 있습니다. * * Controller 파라미터에 Account 객체를 주입합니다. (타입만 일치하면 되고, 파라미터의 순서나 개수는 상관없습니다.) * Account 타입의 파라미터가 존재하지 않더라도 정상적으로 동작합니다.(인증은 요구 하되 정보는 필요없는 경우) @@ -22,7 +22,7 @@ */ @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) -public @interface Authorized { +public @interface RequireRole { @AliasFor("roles") AccountRole[] value() default {}; diff --git a/animory/src/main/java/com/daggle/animory/common/security/SecurityGuard.java b/animory/src/main/java/com/daggle/animory/common/security/SecurityGuard.java index 3efaa628..bd84fa3d 100644 --- a/animory/src/main/java/com/daggle/animory/common/security/SecurityGuard.java +++ b/animory/src/main/java/com/daggle/animory/common/security/SecurityGuard.java @@ -37,7 +37,7 @@ public class SecurityGuard { private static final String AUTHORIZATION_HEADER = "Authorization"; // TODO: 인증 과정의 예외와, 예상치 못한 에러를 구분할 수 있어야 함. - @Around("@within(Authorized) || @annotation(Authorized)") + @Around("@within(com.daggle.animory.common.security.RequireRole) || @annotation(com.daggle.animory.common.security.RequireRole)") public Object validateAuthorization(final ProceedingJoinPoint joinPoint) throws Throwable { try{ final AccountRole[] allowedRoles = getAllowedRoles(joinPoint); @@ -79,7 +79,7 @@ private boolean allowedAllRoles(final AccountRole[] allowedRoles) { private AccountRole[] getAllowedRoles(final ProceedingJoinPoint joinPoint) { // 먼저 메소드 레벨 어노테이션 획득을 시도합니다. final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); - final Authorized methodLevelAnnotation = methodSignature.getMethod().getAnnotation(Authorized.class); + final RequireRole methodLevelAnnotation = methodSignature.getMethod().getAnnotation(RequireRole.class); if (methodLevelAnnotation != null) { return methodLevelAnnotation.value(); @@ -87,7 +87,7 @@ private AccountRole[] getAllowedRoles(final ProceedingJoinPoint joinPoint) { // 메소드 레벨 어노테이션이 없으면 클래스 레벨 어노테이션 획득을 시도합니다.(반드시 존재합니다.) final Class> declaringType = joinPoint.getSignature().getDeclaringType(); - final Authorized classLevelAnnotation = declaringType.getAnnotation(Authorized.class); + final RequireRole classLevelAnnotation = declaringType.getAnnotation(RequireRole.class); return classLevelAnnotation.value(); } diff --git a/animory/src/main/java/com/daggle/animory/domain/pet/controller/PetController.java b/animory/src/main/java/com/daggle/animory/domain/pet/controller/PetController.java index 375d41e0..1d42608f 100644 --- a/animory/src/main/java/com/daggle/animory/domain/pet/controller/PetController.java +++ b/animory/src/main/java/com/daggle/animory/domain/pet/controller/PetController.java @@ -1,7 +1,7 @@ package com.daggle.animory.domain.pet.controller; import com.daggle.animory.common.Response; -import com.daggle.animory.common.security.Authorized; +import com.daggle.animory.common.security.RequireRole; import com.daggle.animory.domain.account.entity.Account; import com.daggle.animory.domain.account.entity.AccountRole; import com.daggle.animory.domain.pet.dto.request.PetRegisterRequestDto; @@ -28,7 +28,7 @@ public class PetController implements PetControllerApi { private final PetWriteService petWriteService; // Pet 등록 - @Authorized(AccountRole.SHELTER) + @RequireRole(AccountRole.SHELTER) @PostMapping(value = "", consumes = {"multipart/form-data"}) public ResponseregisterPet( final Account account, @@ -42,7 +42,7 @@ public Response registerPet( } // Pet 수정 페이지에서, 기존 등록된 정보를 확인하기 위해 호출하는 API - @Authorized(AccountRole.SHELTER) + @RequireRole(AccountRole.SHELTER) @GetMapping(value = "/register-info/{petId}") public Response getPetRegisterInfo(final Account account, @PathVariable final int petId) { @@ -52,7 +52,7 @@ public Response getPetRegisterInfo(final Account account, } // Pet 수정 요청 - @Authorized(AccountRole.SHELTER) + @RequireRole(AccountRole.SHELTER) @PatchMapping(value = "/{petId}", consumes = {"multipart/form-data"}) public Response updatePet( final Account account, @@ -92,7 +92,7 @@ public Response getPetDetail(@PathVariable final int petId) { // Pet 입양 완료 상태 등록 @PostMapping("/adoption/{petId}") - @Authorized(AccountRole.SHELTER) + @RequireRole(AccountRole.SHELTER) public Response updatePetAdopted(final Account account, @PathVariable final int petId) { petWriteService.updatePetAdopted(account, petId);