728x90
✅ Spring Boot에서 CSP 설정
Spring Security를 사용하여 CSP를 적용할 수 있습니다.
Spring Security 설정 예제
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.stereotype.Component;
@Component
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.headers(headers -> headers
.contentSecurityPolicy(csp -> csp.policyDirectives(
"default-src 'self'; " +
"script-src 'self' 'nonce-{nonce}'; " +
"style-src 'self' 'nonce-{nonce}'; " +
"img-src 'self' https://images.example.com; " +
"font-src 'self' https://fonts.googleapis.com; " +
"object-src 'none'; " +
"frame-src 'none'"
))
);
return http.build();
}
}
✔ 설명:
- script-src, style-src에 nonce 값을 적용하여 보안성을 높임
- img-src는 현재 도메인 및 https://images.example.com에서만 로드 가능
- object-src, frame-src는 차단
728x90
'개발 > Spring' 카테고리의 다른 글
| Spring @Primary와 @Qualifier 어노테이션 쉽게 이해하기 (0) | 2025.02.11 |
|---|---|
| Spring에서 @Data 어노테이션 (0) | 2025.02.05 |
| Spring Boot @Validation을 활용한 유효성 검사 (1) | 2025.02.01 |
| Spring 프레임워크에서 Repository 주입 방법 (@Autowired, @Resource, @Inject) (0) | 2025.02.01 |
| Spring AOP(Aspect-Oriented Programming) 가이드 (0) | 2025.01.29 |