使用 Spring RestTemplate 时未设置 'X-Appengine-Inbound-Appid' header

'X-Appengine-Inbound-Appid' header is not set when using Spring RestTemplate

我在 Google App Engine(MS1 和 MS2)上有两个服务 运行。 MS1 部署在标准环境中,MS2 部署在灵活环境中。我正在从标准环境调用 API 到灵活环境。我想确保 MS2 只接受来自 MS1 的请求。因此,我决定使用 App Engine 中的 this 功能。我在 MS1 中将 X-Appengine-Inbound-Appid header 和 setInstanceFollowRedirects 设置为 false,但看起来 App Engine 正在删除此 header。我无法在 MS2 中找到这个 header。

HttpHeaders headers = new HttpHeaders();
headers.add("X-Appengine-Inbound-Appid", ApiProxy.getCurrentEnvironment().getAppId());
HttpEntity<MergePdfsResource> entity = new HttpEntity<MergePdfsResource>(mergePdfsResource, headers);

restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory() {
        protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        super.prepareConnection(connection, httpMethod);
        connection.setInstanceFollowRedirects(false);
    }
});

ResponseEntity<SomeClass> response = restTemplate.postForEntity(apiUrl, entity, SomeClass.class);

以下是我从 Google 支持得到的答案:

仅当我们使用URLFetch 服务时才设置X-Appengine-Inbound-Appid header。在java8 运行时环境中,默认为native,它使用标准Java 网络类。因此,我必须将 URL 流处理程序设置为 appengine-web.xml 中的 urlfetch,如下所示:

<url-stream-handler>urlfetch</url-stream-handler>