상세 컨텐츠

본문 제목

Spring Security 추가 후 H2 console 화면이 표시 안되는 현상 해결 방법

Spring Security

by Wanderer Kim 2025. 6. 28. 20:46

본문

728x90

Spring Security를 이용해 간단한 사이드 프로젝트를 하던 중 H2 console 접속 시 화면이 표시안되는 이슈를 어떻게 수정했는지 공유하겠다.

 

이슈 현상은 아래와 같다.

spring security를 dependency에 추가하고

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.auth0:java-jwt:4.5.0'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

아래와 같이 application properties를 작성하였다.

spring:
  application:
    name: basic-jwt-auth-server
  h2:
    console:
      enabled: true
      path: /h2-console
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:spring
    username: sa
    password:
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        format_sql: true
server:
  port: 8080

 

h2 console에 접속하니 아래와 같은 화면시 표시되었다.

 

한참을 구글링하다 찾은 방법은 의외로 간단했다. 바로 spring securiy 설정 중 frameOption을 disable 시키면 접속이 잘 된다.

 @Bean
 public SecurityFilterChain configure(HttpSecurity http) throws Exception {
        http.csrf(csrf -> csrf.disable())
            .cors(cors -> cors.disable())
            .headers(headers -> headers.frameOptions(FrameOptionsConfig::disable))
            .authorizeHttpRequests(auth -> auth.anyRequest().permitAll());

        return http.build();
 }

 

이유가 궁금해서 검색을 해보니 h2 console은 내부적으로 HTML 페이지를 <iframe>을 통해 구성하고, spring security는 기본적으로 iframe에 의해 렌더링 되는 것을 disable 시키는 것을 알게되었고, 이것을 조정하려면  frameOptions를 사용해야하는것을 알아냈다.

https://docs.spring.io/spring-security/reference/reactive/exploits/headers.html#webflux-headers-frame-options

 

Security HTTP Response Headers :: Spring Security

By default, Spring Security does not add Content Security Policy, because a reasonable default is impossible to know without the context of the application. The web application author must declare the security policies to enforce and/or monitor for the pro

docs.spring.io

나의 경우 개발 목적으로 프로젝트를 만들었으므로 간단하게 frameOption을 비활성화하여 해결하였지만, 실제 운영 환경에서는 해당 옵션을 비활성화하면 안되고 h2같은 memory database가 아닌 MySQL이나 PostgreSQL같은 database 서버를 이용해야 한다.

반응형

관련글 더보기

댓글 영역