首页 > 解决方案 > Jsch:基于隧道的 HTTPS

问题描述

我可以在 Ubuntu 中建立一个端口转发会话,如下所示:

ssh -L 8000:dev.mycompany.com:443 jump.mycompany.com

现在我想用 Jsch 来模拟这个:

    public static void openTunnel() {
    JSch jsch = new JSch();
    String privateKey = "~/.ssh/id_rsa";
    try {
        jsch.addIdentity(privateKey);
        log.info("Connecting to {}@{}", getJumpUser(), getJumpServer());
        Session session = jsch.getSession(getJumpUser(), getJumpServer(), 22);
        session.connect();
        session.setPortForwardingL(8000, getHost(), 433);
    } catch (JSchException e) {
        log.error("", e);
    }
}

但是,在设置隧道后我得到以下异常,并尝试使用 RestTemplate 连接到它(Spring HTTP Client - 但 curl 也给出了错误):

ssl.SSLHandshakeException: Remote host closed connection during handshake
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)

我必须在 Jsch 中配置什么才能使其与 openssh 客户端完全相同?

标签: sshhttpsjsch

解决方案


我想,您正在尝试连接到不公开的 https 网络服务器。

端口转发有效,但 https 在 localhost:8000 上无效,因为证书是 fordev.mycompany.com而不是localhost.

您可以通过在hosts文件中添加条目来作弊。

127.0.0.1 dev.mycompany.com

但可能更容易尝试 socks5。

ssh jump.mycompany.com -D 8005

然后在浏览器中设置(firefox 示例):

选择:手动代理配置:
Socks 主机:localhost
端口:8005
Socks5


推荐阅读