Spring Security와 함께 H2 데이터베이스 및 콘솔 사용하기
작성 일자 : 2024년 09월 29일

개요
Spring Boot 애플리케이션에서 Spring Security를 사용하면서 H2 데이터베이스와 H2 콘솔을 활성화하는 방법에 대해 알아보겠습니다.
H2는 개발 및 테스트 환경에서 많이 사용되는 인메모리 데이터베이스입니다. Spring Security를 사용할 때 H2 콘솔에 접근하려면 몇 가지 설정이 필요합니다.
Spring Security Configuration
아래와 같이 SecurityConfig를 작성합니다.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated());
http.csrf(csrf -> csrf.ignoringRequestMatchers("/h2-console/**"));
http.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
return http.build();
}
}
1. authorizeHttpRequests 설정
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated());
이 부분은 URL 기반의 인가 설정을 구성합니다.
/h2-console/**
경로에 대한 모든 요청을 허용합니다. 이는 H2 콘솔 path에는 인증 없이 접근할 수 있도록 합니다.- 그 외의 모든 요청(
anyRequest()
)은 인증된 사용자만 접근할 수 있습니다.
2. CSRF 설정
http.csrf(csrf -> csrf.ignoringRequestMatchers("/h2-console/**"));
프로젝트에 Spring Security 의존성을 추가하면, 기본적으로 CSRF(Cross-Site Request Forgery) 보호가 활성화 됩니다.
Spring Security에서 CSRF 보호는 인증된 사용자를 대신하여 악성 웹사이트가 무단으로 행동하지 못하도록 방지하는 보안 메커니즘으로, 상태를 변경하는 HTTP 요청(예: POST, PUT, DELETE)에 대해 고유한 토큰을 요구하여 요청이 정상적인 사용자로부터 발생했는지 확인합니다.
H2 콘솔 경로에 대해서는 CSRF 검사를 비활성화하도록 설정합니다.
3. 헤더 설정
http.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
H2 콘솔은 iframe을 사용합니다. 기본적으로 Spring Security는 clickjacking 공격을 방지하기 위해 iframe 사용을 제한합니다.
위의 설정은 같은 출처(origin)에서의 iframe 로드를 허용하여 H2 콘솔이 정상적으로 작동할 수 있게 합니다.
Application Properties 설정
application.yml
파일에 아래와 같이 H2 데이터베이스 설정을 추가합니다.
spring:
h2:
console:
enabled: true
path: /h2-console
datasource:
url: jdbc:h2:mem:testdb
username: sa
password: password
driver-class-name: org.h2.Driver
Spring Security와 함께 H2 데이터베이스 및 콘솔 사용하기
작성 일자 : 2024년 09월 29일

개요
Spring Boot 애플리케이션에서 Spring Security를 사용하면서 H2 데이터베이스와 H2 콘솔을 활성화하는 방법에 대해 알아보겠습니다.
H2는 개발 및 테스트 환경에서 많이 사용되는 인메모리 데이터베이스입니다. Spring Security를 사용할 때 H2 콘솔에 접근하려면 몇 가지 설정이 필요합니다.
Spring Security Configuration
아래와 같이 SecurityConfig를 작성합니다.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated());
http.csrf(csrf -> csrf.ignoringRequestMatchers("/h2-console/**"));
http.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
return http.build();
}
}
1. authorizeHttpRequests 설정
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated());
이 부분은 URL 기반의 인가 설정을 구성합니다.
/h2-console/**
경로에 대한 모든 요청을 허용합니다. 이는 H2 콘솔 path에는 인증 없이 접근할 수 있도록 합니다.- 그 외의 모든 요청(
anyRequest()
)은 인증된 사용자만 접근할 수 있습니다.
2. CSRF 설정
http.csrf(csrf -> csrf.ignoringRequestMatchers("/h2-console/**"));
프로젝트에 Spring Security 의존성을 추가하면, 기본적으로 CSRF(Cross-Site Request Forgery) 보호가 활성화 됩니다.
Spring Security에서 CSRF 보호는 인증된 사용자를 대신하여 악성 웹사이트가 무단으로 행동하지 못하도록 방지하는 보안 메커니즘으로, 상태를 변경하는 HTTP 요청(예: POST, PUT, DELETE)에 대해 고유한 토큰을 요구하여 요청이 정상적인 사용자로부터 발생했는지 확인합니다.
H2 콘솔 경로에 대해서는 CSRF 검사를 비활성화하도록 설정합니다.
3. 헤더 설정
http.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
H2 콘솔은 iframe을 사용합니다. 기본적으로 Spring Security는 clickjacking 공격을 방지하기 위해 iframe 사용을 제한합니다.
위의 설정은 같은 출처(origin)에서의 iframe 로드를 허용하여 H2 콘솔이 정상적으로 작동할 수 있게 합니다.
Application Properties 설정
application.yml
파일에 아래와 같이 H2 데이터베이스 설정을 추가합니다.
spring:
h2:
console:
enabled: true
path: /h2-console
datasource:
url: jdbc:h2:mem:testdb
username: sa
password: password
driver-class-name: org.h2.Driver