SpringBoot

[SpringBoot] 스프링 시큐리티 활성화 후 Post메소드 호출 시 403에러

둥글뉴비 2023. 7. 5. 19:49

Spring Security를 활성화한 후 POST 메서드를 호출할 때 403 오류가 발생했습니다. 이는 CSRF (Cross Site Request Forgery) 설정이 기본적으로 활성화되어 있기 때문에 불가능한 일입니다. 구글링을 통해 오류를 잡을 수 있었으며, 다음과 같은 코드를 사용하여 문제를 해결할 수 있었습니다:

SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .headers()
        .addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN));
    return http.build();
}

 

위의 코드는 CSRF 보호 기능을 비활성화하고 X-Frame-Options 헤더를 설정하여 오류를 해결