I would like to use JSch to create a secure tunnel to the target host and use this tunnel to transport bytes for a mysql connection. But I would not like that this tunnel would be accessible from any other user logged in the same machine.

Is that feasible?

TIA

Leo

example

public class Tunnel {

    /**
     * @param args
     * @throws JSchException 
     * @throws SQLException 
     * @throws ClassNotFoundException 
     */
    public static void main(String[] args) throws JSchException, ClassNotFoundException, SQLException {
        Tunnel t = new Tunnel();
        t.go();
    }

    private void go() throws JSchException, ClassNotFoundException, SQLException {
        JSch.setConfig("StrictHostKeyChecking", "no");
        JSch j = new JSch();
        Session s = j.getSession("mylogin", "192.168.56.101", 22);
        s.setPassword("xxx");
        UI ui = new UI();
        s.setUserInfo(ui);
        int tout = 100;
        s.connect(tout);
        int lport = 9999;
        String host = "localhost";
        int rport = 3306;
        s.setPortForwardingL(lport, host, rport);//open tunnel      

        //this section, localhost accepts mysql connections so it's insecure
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:"+lport+"/DB","root", "xxx");
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("show tables");
        while(rs.next()){
            System.out.println(rs.getString(1));
        }
        rs.close();
        conn.close();
        //end of the insecure section

        s.delPortForwardingL(lport);//close tunnel
        s.disconnect();
        System.out.println("ok");
    }
}