-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
웹소켓 채팅 리팩터링 #160
Merged
웹소켓 채팅 리팩터링 #160
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
31c7d45
feat: 웹소켓 채팅 리팩터링
thdwoqor 5e1d64b
Merge branch 'dev' into feat/159
thdwoqor a7cfaa1
feat: 채팅 롤백 Path 롤백 및 인터셉터 수정
thdwoqor f3939b4
refactor: job skill 수정
thdwoqor dae0ab4
refactor: auth 인증부분 분리
thdwoqor 3847b2a
fix: WebsocketPlayerArgumentResolver 패키지 위치 수
waterricecake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/mafia/mafiatogether/common/interceptor/PathMatcherInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mafia.mafiatogether.common.interceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.simp.stomp.StompCommand; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.messaging.support.ChannelInterceptor; | ||
import org.springframework.messaging.support.MessageHeaderAccessor; | ||
import org.springframework.util.AntPathMatcher; | ||
import org.springframework.util.PathMatcher; | ||
|
||
public class PathMatcherInterceptor implements ChannelInterceptor { | ||
|
||
private final ChannelInterceptor channelInterceptor; | ||
private final PathMatcher pathMatcher; | ||
private final List<StompMapping> includePathPatterns; | ||
private final List<StompMapping> excludePathPatterns; | ||
|
||
public PathMatcherInterceptor(final ChannelInterceptor channelInterceptor) { | ||
this.channelInterceptor = channelInterceptor; | ||
this.pathMatcher = new AntPathMatcher(); | ||
this.includePathPatterns = new ArrayList<>(); | ||
this.excludePathPatterns = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public Message<?> preSend(final Message<?> message, final MessageChannel channel) { | ||
StompHeaderAccessor headerAccessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); | ||
|
||
if (headerAccessor != null && | ||
headerAccessor.getDestination() != null && | ||
thdwoqor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shouldIntercept(headerAccessor.getDestination(), headerAccessor.getCommand())) { | ||
return channelInterceptor.preSend(message, channel); | ||
} | ||
|
||
return ChannelInterceptor.super.preSend(message, channel); | ||
thdwoqor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private boolean shouldIntercept(String destination, StompCommand command) { | ||
boolean isExcluded = excludePathPatterns.stream() | ||
.anyMatch(stompMapping -> matchesPathAndCommand(destination, command, stompMapping)); | ||
|
||
boolean isIncluded = includePathPatterns.stream() | ||
.anyMatch(stompMapping -> matchesPathAndCommand(destination, command, stompMapping)); | ||
|
||
return isIncluded && !isExcluded; | ||
} | ||
|
||
private boolean matchesPathAndCommand(String destination, StompCommand command, StompMapping stompMapping) { | ||
return pathMatcher.match(stompMapping.destination(), destination) && | ||
stompMapping.command() == command; | ||
} | ||
|
||
public PathMatcherInterceptor includePathPattern(String targetPath, StompCommand command) { | ||
this.includePathPatterns.add(new StompMapping(targetPath, command)); | ||
return this; | ||
} | ||
|
||
public PathMatcherInterceptor excludePathPattern(String targetPath, StompCommand command) { | ||
this.excludePathPatterns.add(new StompMapping(targetPath, command)); | ||
return this; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mafia/mafiatogether/common/interceptor/StompMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mafia.mafiatogether.common.interceptor; | ||
|
||
import org.springframework.messaging.simp.stomp.StompCommand; | ||
|
||
public record StompMapping( | ||
String destination, | ||
StompCommand command | ||
) { | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/mafia/mafiatogether/common/resolver/BasicAuthResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/mafia/mafiatogether/common/resolver/WebsocketPlayerArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mafia.mafiatogether.common.resolver; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mafia.mafiatogether.common.annotation.PlayerInfo; | ||
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); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(final MethodParameter parameter, final Message<?> message) throws Exception { | ||
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message); | ||
String authorization = headerAccessor.getFirstNativeHeader("Authorization"); | ||
String[] information = basicAuthResolver.resolve(authorization); | ||
|
||
return new PlayerInfoDto(information[0], information[1]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드 작성하면서 생각하다보니깐 제일 바꾸고 싶었던 영역은 이부분인거같아
기존에는 인터셉터를 통해서 모든 요청에 대해서 위와 같은 URL 패턴이아니면 에러가 발생해서 저런 형태가 아닌 방식으로 pub/sub을 할때 문제가 생겼는데 PathMatcher 를 통해서 필요한 부분만 등록해주는게 필요하다고 생각했어
이전에 확장성 떨어진다고 말한부분이 이부분 이였어
현재 구조에서는 모든요청이 해당 인터셉터를 통과하는 문제
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 말이 구독을 위해서는 "sub/{code}/"이 형식이어야하고 발행은 "pub/{code}/{name}"이 형식이 어색하다는거고 이걸 PatchMatcher의 채인 메서드로 필요할때마다 추가해서 확장성을 높였다는 의미지?
난 이런 의미면 좋은거 같아
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
형식이 어색한것도 있지만 큰문제는
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 기가막힌데??
구우욷!
약간 이 패턴과 COMMAND에 걸리면 우리가 만든 Interceptor 로 보내주는거지?
그렇다면, 뭔가 이런 느낌으로 만들어주고, includePath 할 때 특정 키워드 넣어가지고 어떤 키워드에 매칭 되었을 때, channel Interceptor 에 할당할지도 결정하면 좋을 것 같아!