Spring Boot OAuth2 인증서버 적용기 - 7

황남욱·2021년 12월 19일
0
post-thumbnail

기존에 H2 DB를 Embeded환경으로 사용했다면 이번엔 tcp설정으로 바꿔서 진행해보겠습니다.

H2 설치

저는 Mac으로 개발하고 있으므로 Mac기준으로 작성하겠습니다.

$ brew install h2 // h2 설치 
$ h2 -webAllowOthers  // h2서버 시작
Web Console server running at http://218.38.137.27:8082 (others can connect)
TCP server running at tcp://218.38.137.27:9092 (only local connections)
PG server running at pg://218.38.137.27:5435 (only local connections)

brew install h2 : h2를 homebrew 를 통해 설치합니다.
h2 -webAllowOthers : h2서버를 실행합니다.

http://localhost:8082 접속 시 h2 console이 뜹니다.

위 와 같이 설정하고 연결 시험 혹은 연결을 하면
에러가 발생합니다.

Database "/Users/namookk/test" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-202] 90149/90149 (도움말)

/User/namookk에 test.mv.db 파일이 없어서 발생하는 에러입니다.

에러를 해결하는방법이 2가지 있습니다.

1. jdbc:h2:~/test 로 설정 후 연결 버튼으로 접속하기

위 와 같은 url로 설정 후 연결 시험을 누르지 않고 연결을 바로 눌러서 접속하면 해당 경로에 test.mv.db가 생성됩니다.

저는 1번방법으로 해도 생성이 안되길래... 2번 방법으로 했습니다.

2. /User/namookk에 test.mv.db파일 생성하기

해당 경로에 test.mv.db라는 빈 파일을 생성했더니 접속이 되었습니다.

저는 /OAuth2/oauth.mv.db를 생성 후
jdbc url : jdbc:h2:tcp://localhost/~/OAuth2/oauth
로 접속 하여 사용하겠습니다.



OAuth2인증서버 프로젝트의 application.properties를 수정합니다.

application.properties

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:tcp://localhost/~/OAuth2/oauth;MODE=MySQL;
spring.datasource.username=sa

이제 파일 db를 사용하므로 데이터가 날라가지 않습니다.
client_details에 insert문도 한번만 날리고 ignore해줍니다.

data.sql

insert ignore into oauth_client_details(client_id, resource_ids,client_secret,scope,authorized_grant_types,web_server_redirect_uri,authorities,access_token_validity,refresh_token_validity,additional_information,autoapprove)
values(
       'foo',
       null,
       '{bcrypt}$2a$10$Dp7dXcuT5cGW9clQRfJKIe22EVV7rNCjntXWBE6f0e8nPuu6GlRq6',
       'read,write',
       'authorization_code,refresh_token',
       'http://localhost:1995/oauth2/callback',
       'ROLE_USER',
       36000,
       50000,
       null,
       null
    );

이 후 인증서버를 시작하여 DB에 테이블이 잘 생성되는지 확인하면 됩니다.

잘 생성 되네요. 이후엔 Resource서버를 셋팅해보겠습니다.

profile
안녕하세요👋 주니어 백엔드 개발자입니다.

0개의 댓글