Files
specialsource-homepage-backend/src/main/java/company/specialsource/config/SecurityConfig.java
T
headporter 27d89f35a2
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 54s
fix : 홈페이지 교체로 인한 수정
2026-11-26 13:28:49 +09:00

59 lines
3.0 KiB
Java

package company.specialsource.config;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
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
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable()) // REST API 통신을 위해 CSRF 비활성화
.cors(Customizer.withDefaults()) // 아래 corsConfigurationSource 빈 사용
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
// 로그인 API, 파일 동기화 업로드 API, 메인 화면 IT 헤드라인은 인증 없이 누구나 접근 가능하도록 허용
.requestMatchers("/api/login", "/api/sync/upload", "/api/headlines").permitAll()
.anyRequest().authenticated()
)
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
// 프론트 개발 서버(localhost:3000 등)와 운영 도메인에서의 호출 허용
// (운영은 nginx 경유라 백엔드가 http로 받으므로 https Origin을 다른 출처로 판단함)
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(List.of("http://localhost:*", "https://specialsource.company", "https://www.specialsource.company", "https://new.specialsource.company"));
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
config.setAllowedHeaders(List.of("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", config);
return source;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); // BCrypt 암호화 알고리즘 사용
}
}