diff --git a/src/main/java/com/bluedelivery/api/authentication/AuthenticatedUserArgumentResolver.java b/src/main/java/com/bluedelivery/api/authentication/AuthenticatedUserArgumentResolver.java index 9cdabbc3..a80e174e 100644 --- a/src/main/java/com/bluedelivery/api/authentication/AuthenticatedUserArgumentResolver.java +++ b/src/main/java/com/bluedelivery/api/authentication/AuthenticatedUserArgumentResolver.java @@ -35,6 +35,9 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { + if (AuthenticationHolder.hasAuthentication()) { + return AuthenticationHolder.getAuthentication(); + } HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest(); Optional optional = authenticationService.getAuthentication(req.getHeader(AUTHORIZATION)); if (optional.isPresent()) { diff --git a/src/main/java/com/bluedelivery/api/authentication/AuthenticationHolder.java b/src/main/java/com/bluedelivery/api/authentication/AuthenticationHolder.java new file mode 100644 index 00000000..5e5cb2ab --- /dev/null +++ b/src/main/java/com/bluedelivery/api/authentication/AuthenticationHolder.java @@ -0,0 +1,19 @@ +package com.bluedelivery.api.authentication; + +import com.bluedelivery.domain.authentication.Authentication; + +public class AuthenticationHolder { + private static ThreadLocal authentication = new ThreadLocal<>(); + + public static boolean hasAuthentication() { + return authentication.get() != null; + } + + public static Authentication getAuthentication() { + return authentication.get(); + } + + public static void setAuthentication(Authentication auth) { + authentication.set(auth); + } +} diff --git a/src/main/java/com/bluedelivery/api/authentication/UserAuthInterceptor.java b/src/main/java/com/bluedelivery/api/authentication/UserAuthInterceptor.java index 2eb516ce..bdebcaf9 100644 --- a/src/main/java/com/bluedelivery/api/authentication/UserAuthInterceptor.java +++ b/src/main/java/com/bluedelivery/api/authentication/UserAuthInterceptor.java @@ -9,7 +9,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; @@ -27,20 +26,13 @@ public class UserAuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { - if (needToBeAuthenticated((HandlerMethod) handler)) { + if (Authentication.isAnnotated(((HandlerMethod) handler).getMethod())) { Authentication auth = authenticationService.getAuthentication(request.getHeader(AUTHORIZATION)) .orElseThrow(() -> new ApiException(INVALID_AUTHENTICATION)); if (!isSameUser(request, auth) || auth.isInvalidated()) { throw new ApiException(NOT_AUTHORIZED_ACCESS); } - } - return true; - } - - private boolean needToBeAuthenticated(HandlerMethod handler) { - if (AnnotationUtils.findAnnotation(handler.getMethod(), AuthenticationRequired.class) == null - && AnnotationUtils.findAnnotation(handler.getBeanType(), AuthenticationRequired.class) == null) { - return false; + AuthenticationHolder.setAuthentication(auth); } return true; } diff --git a/src/main/java/com/bluedelivery/domain/authentication/Authentication.java b/src/main/java/com/bluedelivery/domain/authentication/Authentication.java index 5e3a9f89..acf52b07 100644 --- a/src/main/java/com/bluedelivery/domain/authentication/Authentication.java +++ b/src/main/java/com/bluedelivery/domain/authentication/Authentication.java @@ -1,10 +1,15 @@ package com.bluedelivery.domain.authentication; import java.io.Serializable; +import java.lang.reflect.Method; import java.time.Duration; import java.time.Instant; import java.util.Objects; +import org.springframework.core.annotation.AnnotationUtils; + +import com.bluedelivery.api.authentication.AuthenticationRequired; + public class Authentication implements Serializable { public static String AUTH_STR = "auth"; private String token; @@ -21,6 +26,14 @@ public Authentication(String token, Long userId) { this.userId = userId; } + public static boolean isAnnotated(Method method) { + if (AnnotationUtils.findAnnotation(method, AuthenticationRequired.class) == null + && AnnotationUtils.findAnnotation(method.getDeclaringClass(), AuthenticationRequired.class) == null) { + return false; + } + return true; + } + public void invalidate() { this.invalidated = true; }