Skip to content

Commit

Permalink
[feat] websocket 연결 log 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
khee2 committed Jun 12, 2024
1 parent a24722e commit e09ad65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ public class MessageController {
@MessageMapping("/chat/message")
public void message(@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
MessageDto messageDto) {

// Access Token 검증
if (accessToken == null || !jwtTokenProvider.validateToken(accessToken)) { // 메시지 전송 전 유효한 토큰인지 검증
throw new AccessDeniedException("Invalid or expired token");
try {
// Access Token 검증
if (accessToken == null || !jwtTokenProvider.validateToken(accessToken)) { // 메시지 전송 전 유효한 토큰인지 검증
throw new AccessDeniedException("Invalid or expired token");
}
// 메시지 전송 로직 호출
messageRoomService.handleMessage(messageDto.getRoomId(), messageDto.getSender(), messageDto);
} catch (Exception e) {
log.error("Failed to send message: {}", e.getMessage());
throw e;
}

// 메시지 전송 로직 호출
messageRoomService.handleMessage(messageDto.getRoomId(), messageDto.getSender(), messageDto);
}

// 대화 내역 조회
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.SafeNet.Backend.global.config;

import com.SafeNet.Backend.global.auth.JwtTokenProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
Expand All @@ -15,6 +16,7 @@
// WebSocket 메시지의 헤더에서 ACCESS_TOKEN을 추출하고 검증
// 유효한 토큰이 있는 경우 사용자 인증 정보를 설정하고, 유효하지 않은 경우 연결을 차단
@Component
@Slf4j
public class AuthChannelInterceptor implements ChannelInterceptor {

private final JwtTokenProvider jwtTokenProvider;
Expand All @@ -27,18 +29,23 @@ public AuthChannelInterceptor(JwtTokenProvider jwtTokenProvider) {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
if (StompCommand.CONNECT.equals(accessor.getCommand())) { // CONNECT 프레임은 서버에 대한 인증 및 기타 설정과 관련된 정보를 전송하기 위해 사용
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
String token = accessor.getFirstNativeHeader("ACCESS_TOKEN");
if (token != null && token.startsWith("Bearer ")) {
token = token.substring(7);
if (jwtTokenProvider.validateToken(token)) {
String username = jwtTokenProvider.getAuthentication(token).getName();
accessor.setUser(new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList()));
try {
if (token != null && token.startsWith("Bearer ")) {
token = token.substring(7);
if (jwtTokenProvider.validateToken(token)) {
String username = jwtTokenProvider.getAuthentication(token).getName();
accessor.setUser(new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList()));
} else {
throw new IllegalArgumentException("Invalid or expired token");
}
} else {
throw new IllegalArgumentException("Invalid or expired token");
throw new IllegalArgumentException("Missing or invalid ACCESS_TOKEN header");
}
} else {
throw new IllegalArgumentException("Missing or invalid ACCESS_TOKEN header");
} catch (Exception e) {
log.error("Token validation failed: {}", e.getMessage());
throw e;
}
}
return message;
Expand Down

0 comments on commit e09ad65

Please sign in to comment.