2010-08-19 6 views
2

현재 Oracle 데이터베이스에 액세스하려면 SSH를 터널링해야합니다. 이렇게하기 위해서 우리는 응용 프로그램이 Tomcat/Glassfish/etc에 배포되기 전에 퍼티 또는 이와 동등한 프로그램/스크립트가 서버에서 실행되고 있는지 확인해야합니다.SSH 터널을 통해 jdbc의 oracle db에 연결하십시오.

아무도 자바 터널링을 투명하게 처리하는 방법을 찾았습니까? 아마도 Java 자체에서보다 jdbc 드라이버가 자바에서 터널링을 처리하는 다른 jdbc 드라이브를 래핑합니까?

답변

2

내 솔루션 내 응용 프로그램 서버가 시작할 때 터널을 열 JCraft http://www.jcraft.com/jsch/에서 Jsch를 사용하는 것이 었습니다. 응용 프로그램 서버가 종료되면 터널을 닫습니다. 서블릿 컨텍스트 리스너를 통해이 작업을 수행합니다.

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); 
    } 
} 
1

나는 프로젝트를 위해 아파치 MINA SSHD를 사용했고, 터널을 여는 지원이 있다는 것을 기억한다.

자세한 내용은 http://mina.apache.org/sshd/을 확인하십시오.

다른 옵션은이 후 질문에 설명 : SSH library for Java

관련 문제