HTTP 代理配置

如果您位于代理服务器后面,并且需要为 HTTP 出站适配器或网关配置代理设置,您可以使用两种方法之一。在大多数情况下,您可以依赖控制代理设置的标准 Java 系统属性。否则,您可以为 HTTP 客户端请求工厂实例显式配置一个 Spring bean。

标准 Java 代理配置

您可以设置三个系统属性来配置 HTTP 协议处理程序使用的代理设置:

  • http.proxyHost:代理服务器的主机名。

  • http.proxyPort:端口号(默认为 80)。

  • http.nonProxyHosts:应直接访问、绕过代理的主机列表。这是一个由 | 分隔的模式列表。模式可以以 * 开头或结尾,表示通配符。任何与这些模式之一匹配的主机都通过直接连接而不是通过代理访问。

对于 HTTPS,以下属性可用:

  • https.proxyHost:代理服务器的主机名。

  • https.proxyPort:端口号,默认值为 80。

更多信息,请参阅 [role="bare"][role="bare"][role="bare"]docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html

Spring 的 SimpleClientHttpRequestFactory

如果您需要对代理配置进行更明确的控制,您可以使用 Spring 的 SimpleClientHttpRequestFactory 并配置其“proxy”属性,如以下示例所示:

<bean id="requestFactory"
    class="org.springframework.http.client.SimpleClientHttpRequestFactory">
    <property name="proxy">
        <bean id="proxy" class="java.net.Proxy">
            <constructor-arg>
                <util:constant static-field="java.net.Proxy.Type.HTTP"/>
            </constructor-arg>
            <constructor-arg>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg value="123.0.0.1"/>
                    <constructor-arg value="8080"/>
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>