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(); + } +}