Skip to content

Commit f364b07

Browse files
committed
Fix: CORS 설정 보완
1 parent fd41cb7 commit f364b07

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/main/java/com/back/global/security/SecurityConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import lombok.RequiredArgsConstructor;
99
import org.springframework.context.annotation.Bean;
1010
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.http.HttpMethod;
12+
import org.springframework.security.config.Customizer;
1113
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
1214
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1315
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
@@ -35,6 +37,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3537
// 인가 규칙 설정
3638
.authorizeHttpRequests(
3739
auth -> auth
40+
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() // CORS Preflight 요청 허용
3841
.requestMatchers("/api/auth/**", "/oauth2/**", "/login/oauth2/**").permitAll()
3942
.requestMatchers("api/ws/**", "/ws/**").permitAll()
4043
.requestMatchers("/api/rooms/*/messages/**").permitAll() //스터디 룸 내에 잡혀있어 있는 채팅 관련 전체 허용
@@ -59,7 +62,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5962
// JWT 필터 추가
6063
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
6164

62-
// 기타 설정
6365
.headers(
6466
headers -> headers
6567
.frameOptions(
@@ -68,6 +70,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6870
)
6971
.csrf(
7072
AbstractHttpConfigurer::disable
73+
)
74+
.cors(
75+
Customizer.withDefaults()
7176
);
7277

7378
return http.build();
@@ -82,7 +87,7 @@ public void addCorsMappings(CorsRegistry registry) {
8287
.allowedOrigins(
8388
"http://localhost:3000" // Next.js 개발 서버
8489
)
85-
.allowedMethods("GET", "POST", "PUT", "DELETE")
90+
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")
8691
.allowedHeaders("*")
8792
.allowCredentials(true);
8893
}

0 commit comments

Comments
 (0)