From dae0ab446bc17132d66b677a236aa74ee440bc8a Mon Sep 17 00:00:00 2001 From: thdwoqor Date: Thu, 26 Dec 2024 00:18:55 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20auth=20=EC=9D=B8=EC=A6=9D=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/WebMvcConfig.java | 6 ++++- .../common/config/WebSocketConfig.java | 3 ++- .../interceptor/PathMatcherInterceptor.java | 5 ----- .../common/resolver/BasicAuthResolver.java | 22 +++++++++++++++++++ .../resolver/PlayerArgumentResolver.java | 14 ++++++------ .../WebsocketPlayerArgumentResolver.java | 20 ++++++++--------- 6 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 src/main/java/mafia/mafiatogether/common/resolver/BasicAuthResolver.java diff --git a/src/main/java/mafia/mafiatogether/common/config/WebMvcConfig.java b/src/main/java/mafia/mafiatogether/common/config/WebMvcConfig.java index 988dd515..6023c3f8 100644 --- a/src/main/java/mafia/mafiatogether/common/config/WebMvcConfig.java +++ b/src/main/java/mafia/mafiatogether/common/config/WebMvcConfig.java @@ -1,5 +1,6 @@ package mafia.mafiatogether.common.config; +import lombok.RequiredArgsConstructor; import mafia.mafiatogether.common.resolver.PlayerArgumentResolver; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; @@ -10,11 +11,14 @@ import java.util.List; @Configuration +@RequiredArgsConstructor public class WebMvcConfig implements WebMvcConfigurer { + private final PlayerArgumentResolver playerArgumentResolver; + @Override public void addArgumentResolvers(final List resolvers) { - resolvers.add(new PlayerArgumentResolver()); + resolvers.add(playerArgumentResolver); } @Override diff --git a/src/main/java/mafia/mafiatogether/common/config/WebSocketConfig.java b/src/main/java/mafia/mafiatogether/common/config/WebSocketConfig.java index c5435ba2..9ba3d968 100644 --- a/src/main/java/mafia/mafiatogether/common/config/WebSocketConfig.java +++ b/src/main/java/mafia/mafiatogether/common/config/WebSocketConfig.java @@ -20,6 +20,7 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { private final ChatInterceptor chatInterceptor; + private final WebsocketPlayerArgumentResolver websocketPlayerArgumentResolver; @Override public void registerStompEndpoints(StompEndpointRegistry registry) { @@ -34,7 +35,7 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { @Override public void addArgumentResolvers(final List argumentResolvers) { - argumentResolvers.add(new WebsocketPlayerArgumentResolver()); + argumentResolvers.add(websocketPlayerArgumentResolver); } @Override diff --git a/src/main/java/mafia/mafiatogether/common/interceptor/PathMatcherInterceptor.java b/src/main/java/mafia/mafiatogether/common/interceptor/PathMatcherInterceptor.java index 74c4b098..f9e86d32 100644 --- a/src/main/java/mafia/mafiatogether/common/interceptor/PathMatcherInterceptor.java +++ b/src/main/java/mafia/mafiatogether/common/interceptor/PathMatcherInterceptor.java @@ -45,11 +45,6 @@ private boolean shouldIntercept(String destination, StompCommand command) { boolean isIncluded = includePathPatterns.stream() .anyMatch(stompMapping -> matchesPathAndCommand(destination, command, stompMapping)); - System.out.println("##"); - System.out.println("##"); - System.out.println(isExcluded); - System.out.println(isIncluded); - return isIncluded && !isExcluded; } diff --git a/src/main/java/mafia/mafiatogether/common/resolver/BasicAuthResolver.java b/src/main/java/mafia/mafiatogether/common/resolver/BasicAuthResolver.java new file mode 100644 index 00000000..d0552193 --- /dev/null +++ b/src/main/java/mafia/mafiatogether/common/resolver/BasicAuthResolver.java @@ -0,0 +1,22 @@ +package mafia.mafiatogether.common.resolver; + +import lombok.RequiredArgsConstructor; +import mafia.mafiatogether.common.exception.AuthException; +import mafia.mafiatogether.common.exception.ExceptionCode; +import mafia.mafiatogether.common.util.AuthExtractor; +import org.springframework.stereotype.Component; + +@Component +public class BasicAuthResolver { + + public String[] resolve(final String authorization) { + if (authorization == null) { + throw new AuthException(ExceptionCode.MISSING_AUTHENTICATION_HEADER); + } + if (!authorization.startsWith("Basic")) { + throw new AuthException(ExceptionCode.MISSING_AUTHENTICATION_HEADER); + } + + return AuthExtractor.extractByAuthorization(authorization); + } +} diff --git a/src/main/java/mafia/mafiatogether/common/resolver/PlayerArgumentResolver.java b/src/main/java/mafia/mafiatogether/common/resolver/PlayerArgumentResolver.java index 958b8443..2eac36d0 100644 --- a/src/main/java/mafia/mafiatogether/common/resolver/PlayerArgumentResolver.java +++ b/src/main/java/mafia/mafiatogether/common/resolver/PlayerArgumentResolver.java @@ -1,18 +1,23 @@ package mafia.mafiatogether.common.resolver; import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; import mafia.mafiatogether.common.annotation.PlayerInfo; import mafia.mafiatogether.common.exception.AuthException; import mafia.mafiatogether.common.exception.ExceptionCode; -import mafia.mafiatogether.common.util.AuthExtractor; 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 +@RequiredArgsConstructor public class PlayerArgumentResolver implements HandlerMethodArgumentResolver { + private final BasicAuthResolver basicAuthResolver; + @Override public boolean supportsParameter(final MethodParameter parameter) { return parameter.hasParameterAnnotation(PlayerInfo.class); @@ -22,17 +27,12 @@ public boolean supportsParameter(final MethodParameter parameter) { public PlayerInfoDto resolveArgument(final MethodParameter parameter, final ModelAndViewContainer mavContainer, final NativeWebRequest webRequest, final WebDataBinderFactory binderFactory) { HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class); - if (httpServletRequest == null) { throw new AuthException(ExceptionCode.NOT_FOUND_REQUEST); } String authorization = httpServletRequest.getHeader("Authorization"); - if (authorization == null) { - throw new AuthException(ExceptionCode.MISSING_AUTHENTICATION_HEADER); - } - - String[] information = AuthExtractor.extractByAuthorization(authorization); + String[] information = basicAuthResolver.resolve(authorization); return new PlayerInfoDto(information[0], information[1]); } } diff --git a/src/main/java/mafia/mafiatogether/common/resolver/WebsocketPlayerArgumentResolver.java b/src/main/java/mafia/mafiatogether/common/resolver/WebsocketPlayerArgumentResolver.java index fe86a1f6..338e8575 100644 --- a/src/main/java/mafia/mafiatogether/common/resolver/WebsocketPlayerArgumentResolver.java +++ b/src/main/java/mafia/mafiatogether/common/resolver/WebsocketPlayerArgumentResolver.java @@ -1,16 +1,21 @@ package mafia.mafiatogether.chat.ui; +import lombok.RequiredArgsConstructor; import mafia.mafiatogether.common.annotation.PlayerInfo; -import mafia.mafiatogether.common.exception.AuthException; -import mafia.mafiatogether.common.exception.ExceptionCode; +import mafia.mafiatogether.common.resolver.BasicAuthResolver; import mafia.mafiatogether.common.resolver.PlayerInfoDto; -import mafia.mafiatogether.common.util.AuthExtractor; import org.springframework.core.MethodParameter; import org.springframework.messaging.Message; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; +import org.springframework.stereotype.Component; +@Component +@RequiredArgsConstructor public class WebsocketPlayerArgumentResolver implements HandlerMethodArgumentResolver { + + private final BasicAuthResolver basicAuthResolver; + @Override public boolean supportsParameter(final MethodParameter parameter) { return parameter.hasParameterAnnotation(PlayerInfo.class); @@ -20,14 +25,7 @@ public boolean supportsParameter(final MethodParameter parameter) { public Object resolveArgument(final MethodParameter parameter, final Message message) throws Exception { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message); String authorization = headerAccessor.getFirstNativeHeader("Authorization"); - if (authorization == null) { - throw new AuthException(ExceptionCode.MISSING_AUTHENTICATION_HEADER); - } - if (!authorization.startsWith("Basic")) { - throw new AuthException(ExceptionCode.MISSING_AUTHENTICATION_HEADER); - } - - String[] information = AuthExtractor.extractByAuthorization(authorization); + String[] information = basicAuthResolver.resolve(authorization); return new PlayerInfoDto(information[0], information[1]); }