通过SSH隧道连接到jdbc中的Oracle数据库

通过SSH隧道连接到jdbc中的Oracle数据库

本文介绍了通过SSH隧道连接到jdbc中的Oracle数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们必须通过SSH隧道访问我们的Oracle数据库.为此,我们必须确保在将应用程序部署到Tomcat/Glassfish/etc之前,不要在服务器上运行腻子或同等程序/脚本来执行此优化操作.

Currently we have to tunnel over SSH to access our Oracle database. In order to do this we have to make sure than putty or an equivalent program/script is running on the server doing this tunelling before the application is deployed to Tomcat/Glassfish/etc.

有没有人找到让java透明处理此隧道的方法?也许jdbc驱动程序会比其本身包装另一个jdbc驱动器,而该驱动器恰恰是在Java中为您处理隧道的?

Has anybody found a way to have java handle this tunneling transparently? Perhaps a jdbc driver than itself wraps another jdbc drive handling the tunnelling for you right in Java?

推荐答案

我的解决方案是使用来自JCraft的Jsch http://www.jcraft.com/jsch/在我的应用程序服务器启动时打开隧道.当应用程序服务器关闭时,我关闭了隧道.我是通过servlet上下文侦听器完成的.

My solution was to use Jsch from JCraft http://www.jcraft.com/jsch/ to open a tunnel when my application server starts up. I close the tunnel when the application server shuts down. I do this via a servlet context listener.

int findUnusedPort() {
        final int startingPort = 1025;
        final int endingPort = 1200;
        for (int port = 1025; port < 1200; port++) {
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(port);
                return port;
            } catch (IOException e) {
                System.out.println("Port " + port + "is currently in use, retrying port " + port + 1);
            } finally {
                // Clean up
                if (serverSocket != null) try {
                    serverSocket.close();
                } catch (IOException e) {
                    throw new RuntimeException("Unable to close socket on port" + port, e);
                }
            }
        }
        throw new RuntimeException("Unable to find open port between " + startingPort + " and " + endingPort);
    }

private Session doSshTunnel(int tunnelPort) {
    // SSH Tunnel
    try {
        final JSch jsch = new JSch();
        sshSession = jsch.getSession("username", "sshhost", 22);
        final Hashtable<String, String> config = new Hashtable<String, String>();
        config.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(config);
        sshSession.setPassword("password");

        sshSession.connect();

        int assigned_port = sshSession.setPortForwardingL(tunnelPort, remoteHost, remotePort);

        return sshSession;
    } catch (Exception e) {
        throw new RuntimeException("Unable to open SSH tunnel", e);
    }
}

这篇关于通过SSH隧道连接到jdbc中的Oracle数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:03