본문 바로가기
Programming/SpringBoot

[Spring Boot] RESTful Service 강의 정리 (13) - JPA 사용을 위한 Dependency, h2 DataBase 추가와 설정

by prinha 2020. 8. 25.
반응형

 

 

[Spring Boot] RESTful Service 강의 정리 (12) - JPA(Java Persistence API)와 ORM, Hibernate

[Spring Boot] RESTful Service 강의 정리 (11) - Spring Security Configuration 클래스를 이용한 사용자 인증 처리 [Spring Boot] RESTful Service 강의 정리 (10) - Spring Security 자동으로 생성되는 passwor..

prinha.tistory.com


H2DB

H2DB는 자바 기반의 오픈소스 관계형 데이터 베이스 관리 시스템(RDBMS )입니다.

H2DB는 서버(Server) 모드와 임베디드(Embedded) 모드의 인메모리 DB 기능을 지원합니다.

물론 디스크 기반 테이블을 또한 생성할 수 있습니다.

또한 브라우저 기반의 콘솔모드를 이용할 수 있으며,

별도의 설치과정이 없고 용량도 2MB(압축버전) 이하로 매우 저용량 입니다.

DB자체가 매우 가볍기 때문에 매우 가볍고 빠르며, JDBC API 또한 지원하고 있습니다.

SQL 문법은 다른 DBMS들과 마찬가지로 표준 SQL의 대부분이 지원됩니다.

이러한 장점들 때문에 어플리케이션 개발 단계의 테스트 DB로서 많이 이용됩니다.

 

 

H2 Database Engine

H2 Database Engine Welcome to H2, the Java SQL database. The main features of H2 are: Very fast, open source, JDBC API Embedded and server modes; in-memory databases Browser based Console application Small footprint: around 2 MB jar file size     Suppor

www.h2database.com

 



출처: https://dololak.tistory.com/285 [코끼리를 냉장고에 넣는 방법]


1) pom.xml

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <scope>runtime</scope>
</dependency>

 

 

2) application.yml

spring:
  jpa:
    show-sql: true
  h2:
    console:
      enabled: true

 

 

3) Spring Security에서 기본적으로 제공하는 로그인 페이지가 뜸

http://localhost:8088/h2-console -> http://localhost:8088/login

 

 

4) SecurityConfig에 설정했던 유저 정보를 입력하고 연결을 하지만 Spring Security문제로 에러가 뜸

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
   throws Exception{
   auth.inMemoryAuthentication()
        .withUser("Howl")
        .password("{noop}test1234")
        .roles("USER"); // 유저권한부여
}

 

- mem : memory DB / 현재 어플리케이션이 기동되는 동안에만 유지되는 데이터베이스

- 어플리케이션이 종료되도 유지되는 데이터베이스를 원한다면 TCP방식이 지원되는 드라이버 방식으로 바꿔주기

 

 

Spring Security 문제로 에러

 

5) SecurityConfig 설정 다시 해주기

package com.example.restfulwebservice.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // h2 database를 사용하기 위해 메소드 오버라이딩
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/h2-console/**").permitAll();
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
        throws Exception{
        auth.inMemoryAuthentication()
                .withUser("Howl")
                .password("{noop}test1234")
                .roles("USER"); // 유저권한부여
    }
}

 

 

6) 정상적으로 연결된 H2 DataBase

 

 

 

반응형