网络代理后面的 Spring-Boot

问题描述 投票:0回答:6

我目前正在基于 this 示例实施 OpenID 身份验证。现在我正在网络代理后面进行开发,因此服务器无法连接到谷歌。 java代理设置似乎没有任何效果。我还发现了 this stackoverflow 问题,但我不知道将代码放在哪里。如何为我的 Spring Boot 容器配置代理?

谢谢

java spring spring-security proxy spring-boot
6个回答
35
投票

不确定这是否有任何用处,但我目前正在学习 Spring Boot 教程(https://spring.io/guides/gs/integration/)并遇到类似的网络代理问题。只需提供 JVM 参数即可解决此问题

-Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080

16
投票

仅添加提供的两个参数对我来说不起作用。 完整列表如下:

-Dhttp.proxyHost=somesite.com -Dhttp.proxyPort=4321 
-Dhttps.proxyHost=somesite.com -Dhttps.proxyPort=4321 -Dhttps.proxySet=true 
-Dhttp.proxySet=true

1
投票

如果您需要它来调用外部服务,请尝试为您正在使用的客户端(RestTemplate等)设置代理,如下所示:

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

1
投票

我可以用两种方法解决问题

通过 JVM 参数(http 和 https)

-Dhttp.proxyHost=your-http-proxy-host -Dhttp.proxyPort=8080
-Dhttps.proxyHost=your-https-proxy-host -Dhttps.proxyPort=8080

或以编程方式

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

0
投票

对我来说,

server.use-forwarded-headers=true
中的
application.properties
解决了问题。


0
投票

我无法将它们放入属性文件中,但这些 jvm 参数可以添加到 pom.xml 中。添加以下内容:

<build>
   <plugins>
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <jvmArguments>
                  -Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080 
              </jvmArguments>
           </configuration>    
        </plugin>
    </plugins>
</build>

© www.soinside.com 2019 - 2024. All rights reserved.