Skip to content

Commit

Permalink
refactor: auth 인증부분 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
thdwoqor committed Dec 25, 2024
1 parent f3939b4 commit dae0ab4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new PlayerArgumentResolver());
resolvers.add(playerArgumentResolver);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final ChatInterceptor chatInterceptor;
private final WebsocketPlayerArgumentResolver websocketPlayerArgumentResolver;

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
Expand All @@ -34,7 +35,7 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {

@Override
public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new WebsocketPlayerArgumentResolver());
argumentResolvers.add(websocketPlayerArgumentResolver);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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]);
}
}
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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]);
}
Expand Down

0 comments on commit dae0ab4

Please sign in to comment.