diff --git a/backend/src/main/java/com/funeat/auth/dto/LoginRequest.java b/backend/src/main/java/com/funeat/auth/dto/LoginRequest.java new file mode 100644 index 000000000..0479c3932 --- /dev/null +++ b/backend/src/main/java/com/funeat/auth/dto/LoginRequest.java @@ -0,0 +1,14 @@ +package com.funeat.auth.dto; + +public class LoginRequest { + + private final Long id; + + public LoginRequest(final Long id) { + this.id = id; + } + + public Long getId() { + return id; + } +} diff --git a/backend/src/main/java/com/funeat/auth/util/AuthArgumentResolver.java b/backend/src/main/java/com/funeat/auth/util/AuthArgumentResolver.java new file mode 100644 index 000000000..287c139de --- /dev/null +++ b/backend/src/main/java/com/funeat/auth/util/AuthArgumentResolver.java @@ -0,0 +1,36 @@ +package com.funeat.auth.util; + +import com.funeat.auth.dto.LoginRequest; +import java.util.Objects; +import javax.servlet.http.HttpServletRequest; +import org.springframework.core.MethodParameter; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.ModelAndViewContainer; + +@Component +public class AuthArgumentResolver implements HandlerMethodArgumentResolver { + + private final JwtTokenProvider jwtTokenProvider; + + public AuthArgumentResolver(final JwtTokenProvider jwtTokenProvider) { + this.jwtTokenProvider = jwtTokenProvider; + } + + @Override + public boolean supportsParameter(MethodParameter parameter) { + return parameter.hasParameterAnnotation(AuthenticationPrincipal.class); + } + + @Override + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, + NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { + final HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); + final String token = AuthorizationExtractor.extract(Objects.requireNonNull(request)); + final String id = jwtTokenProvider.getPayload(token); + + return new LoginRequest(Long.valueOf(id)); + } +} diff --git a/backend/src/main/java/com/funeat/auth/util/AuthenticationPrincipal.java b/backend/src/main/java/com/funeat/auth/util/AuthenticationPrincipal.java new file mode 100644 index 000000000..a7c0b0cdc --- /dev/null +++ b/backend/src/main/java/com/funeat/auth/util/AuthenticationPrincipal.java @@ -0,0 +1,11 @@ +package com.funeat.auth.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface AuthenticationPrincipal { +} diff --git a/backend/src/main/java/com/funeat/common/WebConfig.java b/backend/src/main/java/com/funeat/common/WebConfig.java index 752d744ee..982eeb666 100644 --- a/backend/src/main/java/com/funeat/common/WebConfig.java +++ b/backend/src/main/java/com/funeat/common/WebConfig.java @@ -1,14 +1,28 @@ package com.funeat.common; +import com.funeat.auth.util.AuthArgumentResolver; +import java.util.List; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { + private final AuthArgumentResolver authArgumentResolver; + + public WebConfig(AuthArgumentResolver authArgumentResolver) { + this.authArgumentResolver = authArgumentResolver; + } + @Override public void addFormatters(final FormatterRegistry registry) { registry.addConverter(new StringToCategoryTypeConverter()); } + + @Override + public void addArgumentResolvers(List resolvers) { + resolvers.add(authArgumentResolver); + } }