전공영역 공부 기록

스프링부트 h2인메모리 콘솔 접속

악분 2021. 9. 9. 22:11
반응형

안녕하세요. 이번 글에서는 스프링부트 h2인메모리 설정 후 콘솔접속하는 방법을 다룹니다.

h2인메모리: https://malwareanalysis.tistory.com/159 참고

 

들어가며

h2에서는 개발 편의성을 위해 h2-console을 제공합니다. h2-console은 GUI로 쿼리문 실행, 데이터 조회 기능을 지원합니다.

 

접속방법

h2 인메모리 방법을 사용했다면 h2-console에 접속하는 일반 방법과 다릅니다. 일반 방법은 h2 console을 실행하고 url등 정보를 입력합니다. 하지만, 인메모리 url에는 들어갈 수가 없습니다. 

 

 

들어가는 방법은 스프링부트를 실행하고 127.0.0.1:<serverport>/h2-console로 접속하시면 됩니다. 스프링부트 실행 로그에도 친절히 설명하고 있습니다. 

스프링부트 실행 로그 중 h2 url출력

 

 

예를 들어서 application.yaml파일이 아래와 같이 설정되어 있다고 가정해봅시다.

소스코드: https://github.com/choisungwook/springsecurity-example/blob/userentity/backend/src/main/resources/application.yaml
server:
  port: 10021

spring:
  datasource:
    url: jdbc:h2:mem:testdb
    driverClassName: org.h2.Driver
    username: sa
    password:
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
  h2:
    console:
      enabled: true

 

server.port가 10021이므로 스프링부트 접속 url은 127.0.0.1:10021입니다. 그리고, h2-console 접속 url은 127.0.0.1:10021/h2-console입니다.

 

 

부록: 스프링시큐리티 설정

스프링시큐리티가 설정되어 있으면 세가지 설정이 필요합니다.

  • /h2-console로 시작하는 접근 모두 허용
  • csrf비활성화
  • http 헤더 frameoptions을 비활성화
package com.sungwook.springbootsecuritydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http 
                .authorizeRequests()            
                    .antMatchers("/h2-console/**").permitAll()                    
                    .anyRequest().authenticated()
                    .and()
                .headers()
                    .frameOptions().disable()
                    .and()                
                .formLogin()
                    .permitAll()
                    .and()                
                .logout()
                    .permitAll()
                    .and()
                .csrf().disable();

    }
}

 

반응형