곧죽어도 콛잉

[Spring] Spring Security에서 H2-Database 사용하기 본문

Spring/기본

[Spring] Spring Security에서 H2-Database 사용하기

코드진행형 2023. 6. 29. 10:53

1. 의존성 추가

Maven Repository: com.h2database » h2

  • 최신 버전 확인
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

2. test용도로 정적 url 설정.

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb

3. 초기 테이블 설정

  • schema.sql 정의하여 초기 테이블 생성하기
create table course
(
    id bigint not null,
    name varchar(255) not null,
    author varchar(255) not null,
    primary key(id)
);
  • Spring Data JPA Starter를 활용할때마다 자동으로 schema.sql 파일을 가져와서 H2에 테이블을 생성함.

4. 만약 Spring-Security를 설정했다면, /h2-console 접속시 다음과 같은 오류 발생함. (h2-console이 frame 태그를 사용하는데, Spring Security가 기본적으로 이것을 막고 있음.)

http.headers().frameOptions().sameOrigin()
// 요청이 동일한 오리진에서 오는 경우, 해당 app의 frame을 허용함.
// example.com은 OK. evil.com은 허용 X.

(Spring Security의 Configuration에서 설정하면된다)

Comments