|
1 |
| -/* |
2 |
| - 데이터베이스 연결 및 SSH 터널링 설정 파일으로 보임. |
3 |
| - */ |
4 |
| - |
5 | 1 | package com.catcher.datasource.config;
|
6 |
| - |
7 | 2 | import com.catcher.infrastructure.KmsService;
|
8 |
| - |
9 |
| -/* for SSH Session */ |
10 | 3 | import com.jcraft.jsch.JSch;
|
11 | 4 | import com.jcraft.jsch.Session;
|
12 |
| - |
13 |
| -/* 생성자 자동 설정 */ |
14 | 5 | import lombok.RequiredArgsConstructor;
|
15 | 6 | import org.springframework.beans.factory.annotation.Value;
|
16 |
| - |
17 |
| -/* DataSource 빌드 (DB 연결 정보 구성 및 DataSource 객체 생성) */ |
18 | 7 | import org.springframework.boot.jdbc.DataSourceBuilder;
|
19 |
| - |
20 |
| -/* bean(?)을 정의하기 위한 어노테이션 */ |
21 | 8 | import org.springframework.context.annotation.Bean;
|
22 |
| - |
23 |
| -/* Spring 어플리케이션의 구성 설정 및 구성요소 생성 .. */ |
24 | 9 | import org.springframework.context.annotation.Configuration;
|
25 | 10 |
|
26 |
| -/* 데이터베이스 연결 관리 */ |
27 | 11 | import javax.sql.DataSource;
|
28 | 12 |
|
29 | 13 | @Configuration
|
@@ -66,30 +50,30 @@ public class DBConfiguration {
|
66 | 50 | @Value("${ssh.local-port}")
|
67 | 51 | private int localPort;
|
68 | 52 |
|
69 |
| -// @Bean |
70 |
| -// public DataSource dataSource() throws Exception { |
71 |
| - |
72 |
| -// JSch jsch = new JSch(); |
73 |
| -// Session session = jsch.getSession( |
74 |
| -// kmsService.decrypt(sshUsername), |
75 |
| -// kmsService.decrypt(sshHost), |
76 |
| -// sshPort |
77 |
| -// ); |
78 |
| -// session.setPassword(kmsService.decrypt(sshPassword)); |
79 |
| -// session.setConfig("StrictHostKeyChecking", "no"); |
80 |
| -// session.connect(); |
81 |
| - |
82 |
| -// int assignedPort = session.setPortForwardingL(0, |
83 |
| -// kmsService.decrypt(originUrl), |
84 |
| -// localPort |
85 |
| -// ); |
86 |
| - |
87 |
| -// return DataSourceBuilder.create() |
88 |
| -// .url(kmsService.decrypt(databaseUrl).replace(Integer.toString(localPort), Integer.toString(assignedPort))) |
89 |
| -// .username(kmsService.decrypt(databaseUsername)) |
90 |
| -// .password(kmsService.decrypt(databasePassword)) |
91 |
| -// .build(); |
92 |
| -// } |
| 53 | + @Bean |
| 54 | + public DataSource dataSource() throws Exception { |
| 55 | + |
| 56 | + JSch jsch = new JSch(); |
| 57 | + Session session = jsch.getSession( |
| 58 | + kmsService.decrypt(sshUsername), |
| 59 | + kmsService.decrypt(sshHost), |
| 60 | + sshPort |
| 61 | + ); |
| 62 | + session.setPassword(kmsService.decrypt(sshPassword)); |
| 63 | + session.setConfig("StrictHostKeyChecking", "no"); |
| 64 | + session.connect(); |
| 65 | + |
| 66 | + int assignedPort = session.setPortForwardingL(0, |
| 67 | + kmsService.decrypt(originUrl), |
| 68 | + localPort |
| 69 | + ); |
| 70 | + |
| 71 | + return DataSourceBuilder.create() |
| 72 | + .url(kmsService.decrypt(databaseUrl).replace(Integer.toString(localPort), Integer.toString(assignedPort))) |
| 73 | + .username(kmsService.decrypt(databaseUsername)) |
| 74 | + .password(kmsService.decrypt(databasePassword)) |
| 75 | + .build(); |
| 76 | + } |
93 | 77 |
|
94 | 78 | @Bean
|
95 | 79 | public DataSource dataSource() {
|
|
0 commit comments