Skip to content

Commit

Permalink
5주차 코드리뷰를 통한 개선안 반영
Browse files Browse the repository at this point in the history
5주차 코드리뷰를 통한 개선안 반영
  • Loading branch information
xGreenNarae authored Oct 17, 2023
2 parents 589a129 + 0040821 commit 94eba46
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,7 @@
* HTTP Request의 Method, URI, Query Parameter를 로깅합니다.*
*/
@Slf4j
@Component
public class RequestLogger implements HandlerInterceptor {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/**
* <pre>
* Authorized가 붙은 Controller의 메소드는 인증된 사용자만 접근할 수 있습니다.
* Annotation이 붙은 Controller의 메소드는 인증된 사용자만 접근할 수 있습니다.
*
* Controller 파라미터에 Account 객체를 주입합니다. (타입만 일치하면 되고, 파라미터의 순서나 개수는 상관없습니다.)
* Account 타입의 파라미터가 존재하지 않더라도 정상적으로 동작합니다.(인증은 요구 하되 정보는 필요없는 경우)
Expand All @@ -22,7 +22,7 @@
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorized {
public @interface RequireRole {

@AliasFor("roles")
AccountRole[] value() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -79,15 +79,15 @@ 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();
}

// 메소드 레벨 어노테이션이 없으면 클래스 레벨 어노테이션 획득을 시도합니다.(반드시 존재합니다.)
final Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
final Authorized classLevelAnnotation = declaringType.getAnnotation(Authorized.class);
final RequireRole classLevelAnnotation = declaringType.getAnnotation(RequireRole.class);

return classLevelAnnotation.value();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 Response<RegisterPetSuccessDto> registerPet(
final Account account,
Expand All @@ -42,7 +42,7 @@ public Response<RegisterPetSuccessDto> registerPet(
}

// Pet 수정 페이지에서, 기존 등록된 정보를 확인하기 위해 호출하는 API
@Authorized(AccountRole.SHELTER)
@RequireRole(AccountRole.SHELTER)
@GetMapping(value = "/register-info/{petId}")
public Response<PetRegisterInfoDto> getPetRegisterInfo(final Account account,
@PathVariable final int petId) {
Expand All @@ -52,7 +52,7 @@ public Response<PetRegisterInfoDto> getPetRegisterInfo(final Account account,
}

// Pet 수정 요청
@Authorized(AccountRole.SHELTER)
@RequireRole(AccountRole.SHELTER)
@PatchMapping(value = "/{petId}", consumes = {"multipart/form-data"})
public Response<UpdatePetSuccessDto> updatePet(
final Account account,
Expand Down Expand Up @@ -92,7 +92,7 @@ public Response<PetDto> getPetDetail(@PathVariable final int petId) {

// Pet 입양 완료 상태 등록
@PostMapping("/adoption/{petId}")
@Authorized(AccountRole.SHELTER)
@RequireRole(AccountRole.SHELTER)
public Response<Void> updatePetAdopted(final Account account,
@PathVariable final int petId) {
petWriteService.updatePetAdopted(account, petId);
Expand Down

0 comments on commit 94eba46

Please sign in to comment.