Recently encountered a problem, that is, to connect to a mysql database remotely
Because this database is distributed on other people's intranets, it is necessary to connect to this database through ssh
Okay, no more nonsense.
first step
Add jar package
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
The second step configures the connection object
package com.wx.util.ssh;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.util.Properties;
/**
* Created by Administrator on 2021/12/1.
*/
public class SSHConnection {
private final static int LOCAl_PORT = 3307; //本地跳转服务器
private final static int REMOTE_PORT = 3306; //远程要转发的端口
private final static int SSH_REMOTE_PORT = 22;
private final static String SSH_USER = "远程服务器用户";
private final static String SSH_PASSWORD = "ssh的密码";
private final static String SSH_REMOTE_SERVER = "中间服务器的ip";
private final static String MYSQL_REMOTE_SERVER = "mysql的服务器的ip";
private Session sesion; //represents each ssh session
public void closeSSH() {
sesion.disconnect();
}
public SSHConnection() throws Throwable {
JSch jsch = null;
jsch = new JSch();
sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);
sesion.setPassword(SSH_PASSWORD);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
sesion.setConfig(config);
sesion.connect(); //ssh connection established!
//by security policy, you must connect through a fowarded port
sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);
}
}
The third step initializes the scan
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletContextListener {
private SSHConnection conexionssh;
public MyListener() {
super();
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Context initialized ... !");
try {
conexionssh = new SSHConnection(); // 监听到了 就装配文件
} catch (Throwable e) {
e.printStackTrace(); // error connecting SSH server
}
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Context destroyed ... !");
conexionssh.closeSSH(); // disconnect
}
}
The fourth step is to modify the configuration file
// 原先配置
base.url=jdbc:mysql://192.168.18.183:3306/data?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useAffectedRows=true
base.username=root
base.password=root
//修改后的配置
base.url=jdbc:mysql://127.0.0.1:3307/data?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
base.username=root
base.password=root
If you want to test, there is also step 5 below
package com.tiaojia;
import com.wx.util.ssh.SSHConnection;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyContextListener implements TestExecutionListener {
private SSHConnection conexionssh;
public MyContextListener() {
super();
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Context initialized ... !");
try {
conexionssh = new SSHConnection();
} catch (Throwable e) {
e.printStackTrace(); // error connecting SSH server
}
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Context destroyed ... !");
conexionssh.closeSSH(); // disconnect
}
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
}
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
System.out.println("Context initialized ... !");
try {
conexionssh = new SSHConnection();
} catch (Throwable e) {
e.printStackTrace(); // error connecting SSH server
}
}
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
}
}
Step 6 Done
you can write your test code
{{o.name}}
{{m.name}}