From 8a2e6720b825d53c96e3d43202be48bdcacc2ae3 Mon Sep 17 00:00:00 2001 From: kimdohyung Date: Thu, 16 Nov 2023 21:02:15 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83?= =?UTF-8?q?=EC=8B=9C=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=9B=EA=B8=B0=20=EC=9C=84=ED=95=9C=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EB=A6=AC=EC=A1=B8=EB=B2=84=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/LoginUserArgumentResolver.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/com/backend/util/LoginUserArgumentResolver.java diff --git a/src/main/java/com/backend/util/LoginUserArgumentResolver.java b/src/main/java/com/backend/util/LoginUserArgumentResolver.java new file mode 100644 index 0000000..5490a29 --- /dev/null +++ b/src/main/java/com/backend/util/LoginUserArgumentResolver.java @@ -0,0 +1,37 @@ +package com.backend.util; + +import com.backend.domain.auth.dto.Login; +import com.backend.domain.auth.dto.LoginUser; +import com.backend.error.ErrorCode; +import com.backend.error.exception.custom.BusinessException; +import org.springframework.core.MethodParameter; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +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; + +public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver { + @Override + public boolean supportsParameter(MethodParameter parameter) { + boolean hasLoginAnnotation = parameter.hasParameterAnnotation(Login.class); + boolean hasLoginUserType = LoginUser.class.isAssignableFrom(parameter.getParameterType()); + + return hasLoginAnnotation && hasLoginUserType; + } + + @Override + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, + NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + + if (authentication == null) { + throw new BusinessException(ErrorCode.USER_NOT_FOUND); + } + + return LoginUser.builder() + .email(authentication.getName()) + .build(); + } +} \ No newline at end of file