当前,我们必须通过SSH隧道访问我们的Oracle数据库。为了做到这一点,我们必须确保在将应用程序部署到Tomcat / Glassfish / etc之前,腻子或等效的程序/脚本正在服务器上运行此调整。
有没有人找到一种让Java透明处理此隧道的方法?也许jdbc驱动程序会比其本身包装另一个jdbc驱动器,而您恰恰在Java中为您处理隧道?
最佳答案
我的解决方案是在我的应用程序服务器启动时使用JCraft http://www.jcraft.com/jsch/中的Jsch打开隧道。当应用程序服务器关闭时,我关闭了隧道。我通过servlet上下文侦听器来完成此操作。
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);
}
}
关于jdbc - 通过SSH隧道连接到jdbc中的Oracle数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3524275/