From da86542940257ecee58271b6cc84ae7f436b5820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B1=B4=EC=9A=B0?= Date: Tue, 29 Jul 2025 17:07:43 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20cors=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../join/member/security/SecurityConfig.java | 20 ++++++++++++++++++- .../handshake/CustomHandshakeHandler.java | 1 - 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/oronaminc/join/member/security/SecurityConfig.java b/src/main/java/com/oronaminc/join/member/security/SecurityConfig.java index 582dac7..8332001 100644 --- a/src/main/java/com/oronaminc/join/member/security/SecurityConfig.java +++ b/src/main/java/com/oronaminc/join/member/security/SecurityConfig.java @@ -2,6 +2,8 @@ import static org.springframework.security.config.Customizer.*; +import java.util.List; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @@ -9,6 +11,9 @@ import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.SecurityFilterChain; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.CorsConfigurationSource; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import lombok.RequiredArgsConstructor; @@ -23,7 +28,7 @@ public class SecurityConfig { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http .csrf(csrf -> csrf.disable()) - .cors(cors -> cors.disable()) + .cors(cors -> cors.configurationSource(corsConfigurationSource())) .authorizeHttpRequests(auth -> auth .requestMatchers( "/api/auth/guest", @@ -51,4 +56,17 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .logout(withDefaults()) .build(); } + + @Bean + public CorsConfigurationSource corsConfigurationSource() { + CorsConfiguration configuration = new CorsConfiguration(); + configuration.setAllowCredentials(true); + configuration.setAllowedOriginPatterns(List.of("*")); + configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); + configuration.setAllowedHeaders(List.of("*")); + + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + return source; + } } diff --git a/src/main/java/com/oronaminc/join/websocket/handshake/CustomHandshakeHandler.java b/src/main/java/com/oronaminc/join/websocket/handshake/CustomHandshakeHandler.java index 2b5b78a..9910dae 100644 --- a/src/main/java/com/oronaminc/join/websocket/handshake/CustomHandshakeHandler.java +++ b/src/main/java/com/oronaminc/join/websocket/handshake/CustomHandshakeHandler.java @@ -58,5 +58,4 @@ protected Principal determineUser(ServerHttpRequest request, WebSocketHandler ws // fallback 경로로 전송 return null; } - } From ea7a8694140402c57d5ac5d116ad5b825486c20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B1=B4=EC=9A=B0?= Date: Wed, 30 Jul 2025 10:16:02 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EC=9B=B9=EC=86=8C=EC=BC=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../join/websocket/config/WebSocketConfig.java | 13 ++++++------- .../session/CurrentParticipantEventHandler.java | 1 - 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java b/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java index a2afca6..0f1cfc2 100644 --- a/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java +++ b/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java @@ -9,7 +9,6 @@ import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory; -import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; import com.oronaminc.join.websocket.handshake.CustomHandshakeHandler; import com.oronaminc.join.websocket.session.CustomWebSocketHandlerDecorator; @@ -48,15 +47,15 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws") .setAllowedOriginPatterns("*") // websocket 연결 전 쿠키 체크 - .addInterceptors(new HttpSessionHandshakeInterceptor()) + // .addInterceptors(new HttpSessionHandshakeInterceptor()) // websocket 연결 후 principal 생성 - .setHandshakeHandler(handshakeHandler) + // .setHandshakeHandler(handshakeHandler) .withSockJS(); - registry.addEndpoint("/ws") - .setAllowedOriginPatterns("*") - .addInterceptors(new HttpSessionHandshakeInterceptor()) - .setHandshakeHandler(handshakeHandler); + // registry.addEndpoint("/ws") + // .setAllowedOriginPatterns("*") + // .addInterceptors(new HttpSessionHandshakeInterceptor()) + // .setHandshakeHandler(handshakeHandler); registry.setErrorHandler(stompErrorHandler); } diff --git a/src/main/java/com/oronaminc/join/websocket/session/CurrentParticipantEventHandler.java b/src/main/java/com/oronaminc/join/websocket/session/CurrentParticipantEventHandler.java index c6b25c2..83d9bf3 100644 --- a/src/main/java/com/oronaminc/join/websocket/session/CurrentParticipantEventHandler.java +++ b/src/main/java/com/oronaminc/join/websocket/session/CurrentParticipantEventHandler.java @@ -2,7 +2,6 @@ import static com.oronaminc.join.global.exception.ErrorCode.*; -import com.oronaminc.join.global.exception.ErrorCode; import java.security.Principal; import java.util.Set; From 9ece1cc4dc9dece270fd405b7e4f819bdf08991d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B1=B4=EC=9A=B0?= Date: Wed, 30 Jul 2025 10:16:45 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EC=9B=B9=EC=86=8C=EC=BC=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oronaminc/join/websocket/config/WebSocketConfig.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java b/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java index 0f1cfc2..797a0c9 100644 --- a/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java +++ b/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java @@ -52,10 +52,11 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { // .setHandshakeHandler(handshakeHandler) .withSockJS(); - // registry.addEndpoint("/ws") - // .setAllowedOriginPatterns("*") - // .addInterceptors(new HttpSessionHandshakeInterceptor()) - // .setHandshakeHandler(handshakeHandler); + registry.addEndpoint("/ws") + .setAllowedOriginPatterns("*") + // .addInterceptors(new HttpSessionHandshakeInterceptor()) + // .setHandshakeHandler(handshakeHandler) + ; registry.setErrorHandler(stompErrorHandler); } From b9b53ad59377e39358efc60d109347df9091ee22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B1=B4=EC=9A=B0?= Date: Wed, 30 Jul 2025 10:46:40 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EC=9B=B9=EC=86=8C=EC=BC=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/QuestionWebsocketController.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/oronaminc/join/websocket/api/QuestionWebsocketController.java b/src/main/java/com/oronaminc/join/websocket/api/QuestionWebsocketController.java index 7186fb2..a0fb291 100644 --- a/src/main/java/com/oronaminc/join/websocket/api/QuestionWebsocketController.java +++ b/src/main/java/com/oronaminc/join/websocket/api/QuestionWebsocketController.java @@ -1,5 +1,13 @@ package com.oronaminc.join.websocket.api; +import java.security.Principal; + +import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.Payload; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; + import com.oronaminc.join.global.exception.ErrorCode; import com.oronaminc.join.global.exception.ErrorException; import com.oronaminc.join.global.ratelimit.RateLimitService; @@ -11,16 +19,11 @@ import com.oronaminc.join.question.dto.QuestionUpdateResponse; import com.oronaminc.join.question.service.QuestionService; import com.oronaminc.join.question.util.QuestionMapper; + import io.github.bucket4j.Bucket; import jakarta.validation.Valid; -import java.security.Principal; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.messaging.handler.annotation.DestinationVariable; -import org.springframework.messaging.handler.annotation.MessageMapping; -import org.springframework.messaging.handler.annotation.Payload; -import org.springframework.messaging.handler.annotation.SendTo; -import org.springframework.stereotype.Controller; @Slf4j @Controller @@ -37,8 +40,12 @@ public QuestionCreateResponse createQuestion( @Payload @Valid QuestionRequest request, Principal principal ) { + log.debug("수신한 메시지 = {}", request.content()); + Long memberId = Long.valueOf(principal.getName()); + log.debug("회원 아이디 = {}", memberId); + Bucket bucket = rateLimitService.getBucket(RateLimitType.CREATE_QUESTION, roomId, memberId); if (!bucket.tryConsume(1)) { @@ -47,7 +54,6 @@ public QuestionCreateResponse createQuestion( Question question = questionService.create(roomId, memberId, request); - log.info("수신한 메시지 = {}", request.content()); return QuestionMapper.toQuestionCreateResponse(question); } From 63cd18c75b2b9d4e2ef37b3fad375c8942837dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B1=B4=EC=9A=B0?= Date: Wed, 30 Jul 2025 10:54:21 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EC=9B=B9=EC=86=8C=EC=BC=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oronaminc/join/websocket/config/WebSocketConfig.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java b/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java index 797a0c9..be7c913 100644 --- a/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java +++ b/src/main/java/com/oronaminc/join/websocket/config/WebSocketConfig.java @@ -9,6 +9,7 @@ import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory; +import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; import com.oronaminc.join.websocket.handshake.CustomHandshakeHandler; import com.oronaminc.join.websocket.session.CustomWebSocketHandlerDecorator; @@ -47,15 +48,15 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws") .setAllowedOriginPatterns("*") // websocket 연결 전 쿠키 체크 - // .addInterceptors(new HttpSessionHandshakeInterceptor()) + .addInterceptors(new HttpSessionHandshakeInterceptor()) // websocket 연결 후 principal 생성 - // .setHandshakeHandler(handshakeHandler) + .setHandshakeHandler(handshakeHandler) .withSockJS(); registry.addEndpoint("/ws") .setAllowedOriginPatterns("*") - // .addInterceptors(new HttpSessionHandshakeInterceptor()) - // .setHandshakeHandler(handshakeHandler) + .addInterceptors(new HttpSessionHandshakeInterceptor()) + .setHandshakeHandler(handshakeHandler) ; registry.setErrorHandler(stompErrorHandler);