Java proxies connection to postgres
Could someo开发者_StackOverflowne help me or suggest a solution? I want to connect from a computer that has firewall to other where the postgres server run. The problem is that computer (client) has a firewall and I don't have access to configure it, or open ports, ping does not respond. The computer (server) where PostgreSQL has open ports but I cannot connect to it from another because of a firewall. I can only access the computer through proxy.
How I could with Java programming access remotely through proxy to postgres forgetting firewall?
Java has a connection with proxies. But I don't know how to put it together with postgres connection.
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "67.210.82.198" );
System.getProperties().put( "proxyPort", "80" );
URL validateURL = new URL("http://domain.com");
URLConnection urlConnection = validateURL.openConnection();
//how put together ???
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://ipPublica:5432/DataBase","user", "pass");
That can't be done. PostgreSQL connections are not HTTP connections. Yo cannot use an HTTP proxy for PostgreSQL. Maybe a socks proxy will do the work.
Try
System.setProperty("http.proxyHost", "67.210.82.198");
System.setPropery("http.proxyPort", "80");
String url = "jdbc:postgresql://host:port/database";
Properties props = new Properties();
props.setProperty("user","myUsername");
props.setProperty("password","myPassword");
props.setProperty("ssl","true");
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, props);
For more, see java networking & proxies.
This question is quite old. It also has been asked on the PostgreSQL mailing list:
http://archives.postgresql.org/pgsql-jdbc/2010-08/msg00021.php
The answers over there boils down to:
Use SOCKs
If (big IF!) you want to hack the JDBC driver source, you might (big "might"!) be able to make it work over a HTTP-Proxy using SSL.
Sidekick: Someone proposed an ssh tunnel.
You'll need a SOCKS proxy, rather than a HTTP proxy.
Then your code will look something like this:
System.setProperty("socksProxyHost", host);
System.setProperty("socksProxyPort", "1080");
Details: http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html
精彩评论