-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from dnd-side-project/dev
�dev -> main
- Loading branch information
Showing
12 changed files
with
184 additions
and
12 deletions.
There are no files selected for viewing
File renamed without changes.
91 changes: 91 additions & 0 deletions
91
src/main/java/org/dnd/timeet/common/interceptor/JwtChannelInterceptor.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,91 @@ | ||
package org.dnd.timeet.common.interceptor; | ||
|
||
import com.auth0.jwt.exceptions.JWTVerificationException; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import com.google.firebase.database.annotations.Nullable; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.dnd.timeet.common.security.CustomUserDetails; | ||
import org.dnd.timeet.common.security.JwtProvider; | ||
import org.dnd.timeet.meeting.application.WebSocketSessionManager; | ||
import org.dnd.timeet.member.application.MemberFindService; | ||
import org.dnd.timeet.member.domain.Member; | ||
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.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* WebSocket 채널에 JWT 검증하는 인터셉터 | ||
*/ | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class JwtChannelInterceptor implements ChannelInterceptor { | ||
|
||
private final MemberFindService userUtilityService; | ||
private final WebSocketSessionManager sessionManager; | ||
|
||
@Override | ||
public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
// 연결 요청시 JWT 검증 | ||
if (StompCommand.CONNECT.equals(accessor.getCommand())) { | ||
// Authorization 헤더 추출 | ||
List<String> authorization = accessor.getNativeHeader(JwtProvider.HEADER); | ||
if (authorization != null && !authorization.isEmpty()) { | ||
String jwt = authorization.get(0).substring(JwtProvider.TOKEN_PREFIX.length()); | ||
try { | ||
// JWT 토큰 검증 | ||
DecodedJWT decodedJWT = JwtProvider.verify(jwt); | ||
Long memberId = decodedJWT.getClaim("id").asLong(); | ||
// 사용자 정보 조회 | ||
Member member = userUtilityService.getUserById(memberId); | ||
|
||
// 사용자 인증 정보 설정 | ||
CustomUserDetails userDetails = new CustomUserDetails(member); | ||
UsernamePasswordAuthenticationToken authentication = | ||
new UsernamePasswordAuthenticationToken( | ||
userDetails, null, userDetails.getAuthorities()); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
// 세션 추가 | ||
String sessionId = accessor.getSessionId(); | ||
sessionManager.addUserSession(sessionId, memberId); | ||
log.info("User Added. Active User Count: " + sessionManager.getActiveUserCount()); | ||
} catch (JWTVerificationException e) { | ||
log.error("JWT Verification Failed: " + e.getMessage()); | ||
return null; | ||
} catch (Exception e) { | ||
log.error("An unexpected error occurred: " + e.getMessage()); | ||
return null; | ||
} | ||
} else { | ||
// 클라이언트 측 타임아웃 처리 | ||
log.error("Authorization header is not found"); | ||
return null; | ||
} | ||
} | ||
return message; | ||
} | ||
|
||
@Override | ||
public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, @Nullable Exception ex) { | ||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
|
||
// 연결 해제 시 세션 정보 제거 | ||
if (StompCommand.DISCONNECT.equals(accessor.getCommand())) { | ||
String sessionId = accessor.getSessionId(); | ||
sessionManager.removeUserSession(sessionId); | ||
log.info("User Disconnected. Active User Count: " + sessionManager.getActiveUserCount()); | ||
} | ||
} | ||
} | ||
|
||
|
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
31 changes: 31 additions & 0 deletions
31
src/main/java/org/dnd/timeet/meeting/application/WebSocketSessionManager.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 org.dnd.timeet.meeting.application; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* 활성 사용자 수 추적 및 세션 관리 | ||
*/ | ||
@Service | ||
public class WebSocketSessionManager { | ||
|
||
private final AtomicInteger activeUserCount = new AtomicInteger(0); | ||
private final Map<String, Long> sessionUserMap = new ConcurrentHashMap<>(); | ||
|
||
public void addUserSession(String sessionId, Long userId) { | ||
sessionUserMap.put(sessionId, userId); | ||
activeUserCount.incrementAndGet(); | ||
} | ||
|
||
public void removeUserSession(String sessionId) { | ||
if (sessionUserMap.remove(sessionId) != null) { | ||
activeUserCount.decrementAndGet(); | ||
} | ||
} | ||
|
||
public int getActiveUserCount() { | ||
return activeUserCount.get(); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/org/dnd/timeet/member/dto/MemberNicknameRequest.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,16 @@ | ||
package org.dnd.timeet.member.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Schema(description = "nickname 등록 요청") | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class MemberNicknameRequest { | ||
|
||
@Schema(description = "nickname", nullable = false, example = "greenfrog") | ||
private String nickname; | ||
} |
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