본문 바로가기
  • A space that records me :)
Framework & Library/Spring & String boot

[Spring] SSH 터널링(Port Forwarding) - gradle

by yjkim_97 2021. 1. 28.

개발 환경

  • Spring Tool Suite 3
  • gradle

bild.gradle

의존성 추가

dependencies {
	
    // ssh 터널링
    // https://mvnrepository.com/artifact/com.jcraft/jsch
	compile("com.jcraft:jsch:0.1.55")
	
}

 

Config 설정

package kr.co.innerwave.quetone.collector.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import kr.co.innerwave.quetone.collector.framework.tunneling.SSHConnection;

@Configuration
public class SshTunnelConfig {

	@Value("${ssh.host}")
	private String SSH_HOST;
	
	@Value("${ssh.user}")
	private String SSH_USER;
	
	@Value("${ssh.pw}")
	private String SSH_PW;
	
	@Value("${local.host}")
	private String LOC_HOST;
	
	@Value("${local.db.port}")
	private int LOC_PORT;
	
	@Value("${remote.db.port}")
	private int REMORT_PORT;
	
	@Profile("!server")
	@Bean
	public SSHConnection sshConnection() {
		SSHConnection conn = new SSHConnection(SSH_HOST,SSH_USER,SSH_PW,LOC_HOST,LOC_PORT,REMORT_PORT);
		conn.init(arg -> {
			if(!arg) {
				System.out.println("!!!!! SSH 터널링 실패 !!!!! 프로그램을 종료");
                System.exit(0);
			}
			else {
				System.out.println("!!!!! SSH 터널링 성공 !!!!!");
			}
		});
		return conn;
	}
}

 

SSH 연결 클래스 구현 - SSHConnection

package kr.co.innerwave.quetone.collector.framework.tunneling;

import java.util.Properties;
import java.util.function.Consumer;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class SSHConnection {
	
	private String SSH_HOST;
	private String SSH_USER;
	private String SSH_PW;
	private String LOC_HOST;
	private int LOC_PORT;
	private int REMORT_PORT;
	
	public SSHConnection(String sshHost, String sshUser, String sshPw, String locHost, int locPort, int remortPort) {
		this.SSH_HOST = sshHost;
		this.SSH_USER = sshUser;
		this.SSH_PW = sshPw;
		this.LOC_HOST = locHost;
		this.LOC_PORT = locPort;
		this.REMORT_PORT = remortPort;
	}
	
	
	private Session sshSession;
	
	public SSHConnection init(Consumer<Boolean> arg) {
		try {
			Properties config = new Properties();
			config.put("StrictHostKeyChecking", "no");
			
			JSch jsch = new JSch();
			sshSession = jsch.getSession(SSH_USER, SSH_HOST);
			sshSession.setPassword(SSH_PW);
			sshSession.setConfig(config);
			
			sshSession.connect();
			sshSession.setPortForwardingL(LOC_PORT,LOC_HOST,REMORT_PORT);
			arg.accept(true);
		}
		catch (Exception e) {
			e.printStackTrace();
			arg.accept(false);
		}
		return this;
	}
	
	public void shutdown() throws Exception {
		if (sshSession != null && sshSession.isConnected()) {
			sshSession.disconnect();
		}
	}
}

connection 정보는 property값으로 설정함.

 

Project main launcher

package kr.co.innerwave.quetone.collector;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import kr.co.innerwave.quetone.collector.crawling.QuetoneCollectorService;

@Configuration
@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = {"kr.co.innerwave.quetone"})
public class Launcher implements CommandLineRunner
{
	
	@Autowired(required = false)
	SSHConnection sshConnection;
	
	@Autowired
	QuetoneCollectorService quetoneCollectorService;
	
    public static void main(String[] args)
    {
        SpringApplication.run(Launcher.class, args);
    }
    
    @PreDestroy
    public void end() {
    	try {
    		if (sshConnection != null)
    		sshConnection.shutdown();
    	}
    	catch (Exception e) {
    		e.printStackTrace();
		}
    }

	@Override
	public void run(String... args) throws Exception {

	}

}