From 1b7f8a0a3066b3f2cf853528cae7eba623e719e6 Mon Sep 17 00:00:00 2001 From: Kwak Seong Joon Date: Tue, 28 May 2024 00:51:58 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20UserIdArgumentResolver=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/HandlerMethodArgumentResolver.java | 14 +++++++++++ .../common/jwt/auth/UserId.java | 11 ++++++++ .../jwt/auth/UserIdArgumentResolver.java | 25 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/HandlerMethodArgumentResolver.java create mode 100644 springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserId.java create mode 100644 springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserIdArgumentResolver.java diff --git a/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/HandlerMethodArgumentResolver.java b/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/HandlerMethodArgumentResolver.java new file mode 100644 index 0000000..aea06f9 --- /dev/null +++ b/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/HandlerMethodArgumentResolver.java @@ -0,0 +1,14 @@ +package org.sopt.springFirstSeminar.common.jwt.auth; + +import org.springframework.core.MethodParameter; +import org.springframework.lang.Nullable; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.method.support.ModelAndViewContainer; + +public interface HandlerMethodArgumentResolver { + boolean supportsParameter(MethodParameter parameter); + + @Nullable + Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception; +} diff --git a/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserId.java b/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserId.java new file mode 100644 index 0000000..5aff35d --- /dev/null +++ b/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserId.java @@ -0,0 +1,11 @@ +package org.sopt.springFirstSeminar.common.jwt.auth; + +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 UserId { +} diff --git a/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserIdArgumentResolver.java b/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserIdArgumentResolver.java new file mode 100644 index 0000000..881bceb --- /dev/null +++ b/springFirstSeminar/src/main/java/org/sopt/springFirstSeminar/common/jwt/auth/UserIdArgumentResolver.java @@ -0,0 +1,25 @@ +package org.sopt.springFirstSeminar.common.jwt.auth; + +import org.springframework.core.MethodParameter; +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.ModelAndViewContainer; + +public class UserIdArgumentResolver implements HandlerMethodArgumentResolver { + + // + @Override + public boolean supportsParameter(MethodParameter parameter) { + boolean hasUserIdAnnotation = parameter.hasParameterAnnotation(UserId.class); + boolean isLongType = Long.class.isAssignableFrom(parameter.getParameterType()); + return hasUserIdAnnotation && isLongType; + } + + @Override + public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { + return SecurityContextHolder.getContext() + .getAuthentication() + .getPrincipal(); + } +}