Spring-Boot behind a network proxy - java

I am currently implementing an OpenID authentication based on this example. Now I am developing behind a network proxy, therefore the server cannot connect to google. The java proxy settings seem to not have any effect. I also found this stackoverflow question, but I cannot figure out where to put the code. How can I configure the proxy for my spring boot container?
thanks

Not sure if this is of any use, but I'm just working through a Spring Boot tutorial currently (https://spring.io/guides/gs/integration/) and hit a similar network proxy issue. This was resolved just by providing the JVM arguments
-Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080

Adding just the two provided arguments didn't work for me.
Full list that did it is this:
-Dhttp.proxyHost=somesite.com -Dhttp.proxyPort=4321
-Dhttps.proxyHost=somesite.com -Dhttps.proxyPort=4321 -Dhttps.proxySet=true
-Dhttp.proxySet=true

If you need this to make a call to an external service, then try to set proxy to the Client you are using (RestTemplate, etc), as below:
HttpComponentsClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory();
DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
HttpHost proxy = new HttpHost("proxtserver", port);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
restTemplate.setRequestFactory(requestFactory);

For me, server.use-forwarded-headers=true in application.properties solved the problem.

I could able to solve the problem in two methods
Through JVM args (both http & https)
-Dhttp.proxyHost=your-http-proxy-host -Dhttp.proxyPort=8080
-Dhttps.proxyHost=your-https-proxy-host -Dhttps.proxyPort=8080
Or Programatically
public static void setProxy() {
System.setProperty("http.proxyHost", "your-http-proxy-host");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "your-http-proxy-host");
System.setProperty("https.proxyPort", "8080");
}

Related

Cannot login into Salesforce with Java using Proxy Server

I wrote Java code to login to Salesforce and ran this code on a firewalled server. For this, I have to specify the proxy url and proxy port before connecting to Salesforce due to the firewall. However, I'm getting an unknownhostexception error for the proxy url. If I try to login via curl with the proxy settings, I am able to connect. How come there is a problem connecting using Java then? Any help is appreciated.
Apparently the issue was caused by JVM configs. We have to configure JVM to use the proxy settings as follows:
System.setProperty("http.proxyHost", crmProxyURL);
System.setProperty("http.proxyPort", crmProxyPort);
Depending on your HTTP library the System settings might not be enough or not needed. In the Salesforce context it is quite possible that one would try using the Jetty HTTP Client. In this case the System properties are ignored and proxy needs to be handled by the Jetty client:
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
HttpProxy proxy = new HttpProxy("proxyHost", proxyPort);
proxyConfig.getProxies().add(proxy);
The Apache HTTP Client, another popular choice, also uses its own little mechanism:
HttpHost proxy = new HttpHost("proxyHost", proxyPort, "https");
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet request = new HttpGet(someURL);
request.setConfig(config);
Interesting here: one can specify to use http or https as the proxy protocol.
In general I found using one of the http client much easier that the JDK low level functions.

how to set proxy for firefox using java?

I am trying to develop an app which will take ip address as a input and set it as a proxy in clients firefox as a proxy. The idea here is , I am trying to gain access to squid server through java? is there any possibility of doing so? Thanks in advance
You can configure your Java application to use Charles in code or as command line arguments to the java executable.
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8888");
And for HTTPS as well. Note that you may also want to configure Java to trust Charles’s root certificate in this case (see SSL Proxying).
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "8888");
proxy settings for Firefox are stored in prefs.js file of user
%APPDATA%\Mozilla\Firefox\Profiles\7a3fd5zw.default\prefs.js
here 7a3fd5zw is a random string.
There are settings named "network.proxy.http" and "network.proxy.http_port".
May be, you can try to modify them.

Java HTTP proxy wont connect

How to connect properly using HTTP proxy in java? I can do it with sockProxyHost, but not with http.proxyHost.
If i do with socks, it will wait out for the proxy to connect and then it will create the web request, but if i do it with http it will just create the request and dont connect to the proxy.
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "validproxyip");
System.setProperty("http.proxyPort", "validproxyport");
is my code. but it wont connect to the proxy. Please help!
Check out the proxy documentation at Oracle
Either you have a typo, or you are using https and not http, or you access a "No-proxy" host. If you want to be sure, use the ProxySelector.
Try set it in the vm args
java -DproxyHost=proxy.mydomain.com -DproxyPort=3128 your.Main

How to configure java rome fetcher for use with a proxy and authentication

I'm trying to use java rome-fetcher to acquire rss feeds for processing. Everything works fine when I have direct internet access.
However, I need to be able to run my application behind a proxy server.
I have been unable to figure out how this can be done with rome-fetcher.
I am aware of the jvm
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
hack, but that is not an option for reasons I don't really want to explain.
With HttpClient you typically do something like this.
DefaultHttpClient client = new DefaultHttpClient();
HttpHost proxyTarget = new HttpHost("proxy.server.com", 4444);
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyTarget);
Does anyone how to assign proxy settings, and authentication credentials for that matter, to rome-fetcher?
Setting http.proxyHost and http.proxyPort is the only option to use http proxy for Rome for the time being.
Because the System.setProperty(...) is the only proxy option for rome-fetcher I ended up downloading a copy of the rome-fetcher source and made modifications to the underlying http client so it can handle different proxy configurations.
Fetcher was deprecated in version 1.6 of Rome and will be removed in version 2.0:
https://github.com/rometools/rome/issues/276
One of the reasons given is that the user doesn't have full control over the underlying HTTP connection -- an example being the inability to specify a proxy. Directly using Apache HttpClient is suggested instead.

Using fiddler with Java and Eclipse

I'm trying to hook up fiddler to a java unit test in Eclipse so I can see the soap request when our web service is being called...It works automatically in our .NET harness but is there some setting that needs to be applied for Java? Thanks
I have not tried this, but ...
Fiddler establishes itself as a proxy server, listening on localhost:8888
You can configure Java to use a proxy server with the http.proxyHost and http.proxyPort (see http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html).
So, if you go into Eclipse and set the "VM" arguments to the following, it should route all traffic through Fiddler (which, of course, must be already running):
-Dhttp.proxyHost=localhost
-Dhttp.proxyPort=8888
This assumes that your application is using URLConnection. If it's using Apache HttpClient or some other library, you may need to check the documentation for that library.
I am usring Apache HttpClient(4.5.5), SWT 4, Fiddler 4, and the VM arguments method does not work for me. So I set the proxy settings in the code and it works.
HttpHost proxy = new HttpHost("localhost", 8888, "http");
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();

Categories