' X-的AppEngine-入站-APPID'使用Spring RestTemplate时未设置标头

时间:2018-03-26 04:07:33

标签: spring google-app-engine spring-restcontroller spring-rest google-flexible

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

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

1 个答案:

答案 0 :(得分:3)

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

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

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