-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 웹소켓 채팅 리팩터링 * feat: 채팅 롤백 Path 롤백 및 인터셉터 수정 * refactor: job skill 수정 * refactor: auth 인증부분 분리 * fix: WebsocketPlayerArgumentResolver 패키지 위치 수 --------- Co-authored-by: waterricecake <[email protected]>
- Loading branch information
1 parent
0cb1952
commit 501f1d4
Showing
12 changed files
with
210 additions
and
32 deletions.
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 && | ||
shouldIntercept(headerAccessor.getDestination(), headerAccessor.getCommand())) { | ||
return channelInterceptor.preSend(message, channel); | ||
} | ||
|
||
return ChannelInterceptor.super.preSend(message, channel); | ||
} | ||
|
||
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.