Skip to content
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

[feat] websocket 연결 log 추가 #28

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading