Google App Engine Firefox和IE缺少HTTP Referer标头

时间:2017-03-19 15:01:02

标签: angular google-app-engine google-cloud-endpoints

向App Engine(标准)上运行的Cloud Endpoints API发出HTTP请求时,服务器出错。 Stackdriver日志显示:

InvalidResponseError: status must be a str, got 'unicode' (u'403 Requests from referer <empty> are blocked.')" 

我正在使用自动生成的Angular2,Typescript客户端,使用Swagger's online editor生成。生成的代码在发出请求时使用以下Angular2模块:

import { Http, Headers, URLSearchParams }                    from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Response, ResponseContentType }                     from '@angular/http';

一切都适用于Chrome,但不适用于Firefox和IE。作为实验,我在Angular2客户端发出HTTP请求之前删除了任何标头(headers:undefined

let requestOptions: RequestOptionsArgs = new RequestOptions({
            method: RequestMethod.Post,
            headers: undefined,
            body: body == null ? '' : JSON.stringify(body),
            search: queryParameters
});

this.http.request(path, requestOptions);

这解决了问题,Firefox和IE工作正常。在将headers设置为undefined之前,添加的标题为:

headers.set('Content-Type', 'application/json');

查看Chrome和Firefox的HTTP标头,Firefox缺少Referer

Firefox请求标头(缺少Referer

Host: api.endpoints.myapp.appspot.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://localhost:4200
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Chrome请求标头

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.endpoints.myapp.appspot.com
Origin:http://localhost:4200
Referer:http://localhost:4200/editor
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

由于Google App Engine API使用API​​密钥和HTTP引荐来源白名单限制访问:

...空引用者导致HTTP请求被拒绝。问题:

  • 为什么要删除angular 2客户端中的整个标头,导致引用器被填充?
  • 如果在限制API密钥访问时Google App Engine拒绝空的引用者,我应该如何通过客户端保护我的API(即不是用户身份验证)。

0 个答案:

没有答案