20250805 - 학습일지

창훈·2025년 8월 5일

스프링시큐리티 적용

의존성 추가

(... 생략 ...)

dependencies {
    (... 생략 ...)
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
}

(... 생략 ...)
  • 스프링시큐리티와 스프링부츠 버젼에 맞추어 사용되어야 함

root 폴더 내에 SecurityConfig.java 추가

  • SecurityConfig.java를 추가하기 전에는 접근시 아래의 화면이 전시된다
  • 이때 아래의 생성 비밀번호를 입력하여 접근가능하다.
package com.mysite.sbb;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
// import org.springframework.*;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests((authorizeHttpRequests) -> authorizeHttpRequests
                        .requestMatchers(new AntPathRequestMatcher("/**")).permitAll())
                .csrf((csrf) -> csrf
                        .ignoringRequestMatchers(new AntPathRequestMatcher("/h2-console/**")))
        ;
        return http.build();
    }
}
  • AntPathRequestMatcher("/**").permitAll() 구문은 모든 root directory 에 대한 접근을 허가한 내용이다.

CSRF(Cross-Site Request Forgery) 검증.

 .csrf((csrf) -> csrf
                        .ignoringRequestMatchers(new AntPathRequestMatcher("/h2-console/**")))

X-Frame-Options는 웹 페이지가 iframe, frame, object 태그 안에서 렌더링될 수 있는지를 제어하는 HTTP 응답 헤더입니다. 주로 클릭재킹(clickjacking) 공격을 방지하기 위해 사용됩니다.

              .headers((headers) -> headers
                      .addHeaderWriter(new XFrameOptionsHeaderWriter(
                              XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)))

언제 어떤 옵션을 써야 할까?

  • 공개 웹사이트: DENY 권장
  • 내부 시스템에서 iframe 사용: SAMEORIGIN
  • 특정 외부 도메인에서 iframe 허용: ALLOW-FROM은 비추천 → 대신 Content-Security-Policy: frame-ancestors 사용
profile
한줄소개불가

0개의 댓글