Spring-Boot behind a network proxy

2020-02-10 06:17发布

问题:

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

回答1:

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


回答2:

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


回答3:

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



回答4:

If you are using Intellij go to this file C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2\plugins\maven\lib\maven3\conf

Change security settings so you can edit.

<proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username></username>
      <password></password>
      <host>Enter Your Host Here</host>
      <port>8080</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    <proxy>
      <id>optional2</id>
      <active>true</active>
      <protocol>https</protocol>
      <username></username>
      <password></password>
      <host>Enter Your Host Here</host>
      <port>8080</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>


回答5:

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