使用jsch 实现ssh tunnel
作者:互联网
主要目的是解决比如访问敏感信息,我们通过加密隧道实现数据访问,而且对于数据进行加密
参考图
参考代码
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>ssh-tunnel</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</project>
- 代码
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class App {
public void go() throws Exception {
String host = "<sship>";
String user ="<user>>";
String password = "<password>";
int port = 22;
int tunnelLocalPort = 9080;
String tunnelRemoteHost = "<containerip>";
int tunnelRemotePort = 3306;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
LocalUserInfo lui = new LocalUserInfo();
session.setUserInfo(lui);
session.connect();
session.setPortForwardingL(tunnelLocalPort, tunnelRemoteHost, tunnelRemotePort);
System.out.println("Connected");
}
public static void main(String[] args) throws Exception {
App app = new App();
app.go();
}
}
说明
以上代码能实现很简单,但是可以解决我们实际对于加密信息的访问,实际上好多基于容器的调试也会选择基于ssh tunnel 实现
参考资料
标签:jsch,http,String,tunnel,session,ssh,jcraft,com 来源: https://www.cnblogs.com/rongfengliang/p/15845175.html