feat : 로컬 프론트 개발 서버용 CORS 허용 (http://localhost:*)
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 2m2s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
headporterandClaude Opus 5.5 committed 2026-11-17 14:58:08 +09:00
1 parent d7547a1028
commit c9590e91a9
1 file changed
+19
@@ -10,6 +10,12 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.config.Customizer;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.List;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@@ -22,6 +28,7 @@ public class SecurityConfig {
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http http
.csrf(csrf -> csrf.disable()) // REST API 통신을 위해 CSRF 비활성화 .csrf(csrf -> csrf.disable()) // REST API 통신을 위해 CSRF 비활성화
.cors(Customizer.withDefaults()) // 아래 corsConfigurationSource 빈 사용
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth .authorizeHttpRequests(auth -> auth
// 로그인 API 및 파일 동기화 업로드 API는 인증 없이 누구나 접근 가능하도록 허용 // 로그인 API 및 파일 동기화 업로드 API는 인증 없이 누구나 접근 가능하도록 허용
@@ -32,6 +39,18 @@ public class SecurityConfig {
return http.build(); return http.build();
} }
// 프론트 개발 서버(localhost:3000 등)에서의 호출 허용
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(List.of("http://localhost:*"));
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
config.setAllowedHeaders(List.of("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", config);
return source;
}
@Bean @Bean
public PasswordEncoder passwordEncoder() { public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); // BCrypt 암호화 알고리즘 사용 return new BCryptPasswordEncoder(); // BCrypt 암호화 알고리즘 사용