diff --git a/src/main/java/mafia/mafiatogether/common/config/WebMvcConfig.java b/src/main/java/mafia/mafiatogether/common/config/WebMvcConfig.java index 988dd51..6023c3f 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 c5435ba..9ba3d96 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 74c4b09..f9e86d3 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 0000000..d055219 --- /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 958b844..2eac36d 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 fe86a1f..338e857 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]); }