使用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 实现

参考资料

http://www.jcraft.com/jsch/

posted on   荣锋亮  阅读(230)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-01-26 cube.js join 的处理
2021-01-26 cube.js 一个隐藏的schema 扩展服务
2021-01-26 cube.js schemaversion的处理
2021-01-26 扩展cube.js的repositoryFactory 支持基于s3的schema 存储
2020-01-26 deno学习五 typescript + vscode 集成
2020-01-26 makeself 试用
2017-01-26 好的产品经理,差的产品经理

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示