66import org .springframework .context .annotation .Bean ;
77import org .springframework .context .annotation .Configuration ;
88import org .springframework .http .HttpHeaders ;
9- import org .springframework .http .HttpMethod ; // ✅ 추가
109import org .springframework .http .MediaType ;
1110import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1211import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
2019import org .springframework .web .reactive .function .client .WebClient ;
2120import util .JwtTokenProvider ;
2221
23- // ⚠️ JwtAuthenticationFilter import는 프로젝트 경로에 맞게 유지하세요.
24- // import com.dmu.debug_visual.config.JwtAuthenticationFilter; (예시)
25-
2622import java .util .List ;
2723
2824@ Configuration
@@ -39,40 +35,24 @@ public class SecurityConfig {
3935 @ Bean
4036 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
4137 http
42- // CORS 우선 적용
43- .cors (cors -> cors .configurationSource (corsConfigurationSource ()))
44- // REST API라면 CSRF 비활성
45- .csrf (csrf -> csrf .disable ())
46- .authorizeHttpRequests (auth -> auth
47- // ✅ 프리플라이트 전역 허용 (중요)
48- .requestMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
49-
50- // ✅ 공개 엔드포인트
51- .requestMatchers (
52- "/api/users/login" ,
53- "/api/users/signup" ,
54- "/swagger-ui/**" ,
55- "/swagger-ui.html" ,
56- "/v3/api-docs/**" ,
57- "/actuator/health" ,
58- "/healthz" ,
59- "/public/**"
60- ).permitAll ()
61-
62- // 역할 기반
63- .requestMatchers ("/api/admin/**" ).hasRole ("ADMIN" )
64- .requestMatchers ("/api/posts/**" ).hasAnyRole ("USER" , "ADMIN" )
65- .requestMatchers ("/api/notifications/**" ).hasAnyRole ("USER" , "ADMIN" )
66-
67- // 나머지는 인증 필요
68- .anyRequest ().authenticated ()
69- )
70- // JWT 필터 (위치 유지)
71- .addFilterBefore (new JwtAuthenticationFilter (jwtTokenProvider , userRepository ),
72- UsernamePasswordAuthenticationFilter .class )
73- // 폼/기본 인증 비활성
74- .formLogin (form -> form .disable ())
75- .httpBasic (basic -> basic .disable ());
38+ .cors (cors -> cors .configurationSource (corsConfigurationSource ()))
39+ .csrf (csrf -> csrf .disable ())
40+ .authorizeHttpRequests (auth -> auth
41+ .requestMatchers (
42+ "/api/users/login" ,
43+ "/api/users/signup" ,
44+ "/swagger-ui/**" ,
45+ "/v3/api-docs/**"
46+ ).permitAll ()
47+ .requestMatchers ("/api/admin/**" ).hasRole ("ADMIN" )
48+ .requestMatchers ("/api/posts/**" ).hasAnyRole ("USER" , "ADMIN" )
49+ .requestMatchers ("/api/notifications/**" ).hasAnyRole ("USER" , "ADMIN" )
50+ .anyRequest ().authenticated ()
51+ )
52+ .addFilterBefore (new JwtAuthenticationFilter (jwtTokenProvider , userRepository ),
53+ UsernamePasswordAuthenticationFilter .class )
54+ .formLogin (form -> form .disable ())
55+ .httpBasic (basic -> basic .disable ());
7656
7757 return http .build ();
7858 }
@@ -82,7 +62,6 @@ public PasswordEncoder passwordEncoder() {
8262 return new BCryptPasswordEncoder ();
8363 }
8464
85- // 백엔드 → 파이썬 컴파일러 호출용 WebClient
8665 @ Bean
8766 public WebClient webClient () {
8867 return WebClient .builder ()
@@ -91,22 +70,15 @@ public WebClient webClient() {
9170 .build ();
9271 }
9372
94- // ✅ CORS: 프록시(Nginx)와 값 일치 — zivorp.com만 허용(+ www, http/https)
9573 @ Bean
9674 public CorsConfigurationSource corsConfigurationSource () {
9775 CorsConfiguration config = new CorsConfiguration ();
98- // AllowedOriginPatterns("*") + Credentials(true)는 브라우저에서 막히기 쉬움.
99- // 명시적으로 허용 도메인만 지정하세요.
100- config .setAllowedOrigins (List .of (
101- "https://zivorp.com" ,
102- "http://zivorp.com" ,
103- "https://www.zivorp.com" ,
104- "http://www.zivorp.com"
105- ));
106- config .setAllowedMethods (List .of ("GET" , "POST" , "PUT" , "PATCH" , "DELETE" , "OPTIONS" ));
107- config .setAllowedHeaders (List .of ("Authorization" , "Content-Type" , "X-Requested-With" ));
76+ config .setAllowedOriginPatterns (List .of ("*" ));
77+ // 또는 특정 도메인만 허용할 경우:
78+ // config.setAllowedOrigins(List.of("https://zivorp.com", "http://zivorp.com"));
79+ config .setAllowedMethods (List .of ("GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ));
80+ config .setAllowedHeaders (List .of ("*" ));
10881 config .setAllowCredentials (true );
109- config .setMaxAge (3600L );
11082
11183 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
11284 source .registerCorsConfiguration ("/**" , config );
0 commit comments