我在 Google App Engine 上有两个应用,都在同一个帐户下运行,一个通过 HTTPS 调用另一个提供的服务。确保只有第一个应用程序被允许调用第二个应用程序的推荐方法是什么?
或者,有没有办法指定给定的端点只能由在同一 GAE 帐户下运行的应用程序调用?
最佳答案
让您的应用检查“X-Appengine-Inbound-Appid” header 并确保应用 ID 正确。此 header 仅在请求由另一个 Google App Engine 应用程序发出且用户无法修改时才存在。
如果您使用的是 Python,您可以执行以下操作:
import webapp2
AUTHORIZED_APPS = ('my-first-app', 'my-other-app')
class MyHandler(webapp2.RequestHandler):
def dispatch(self):
app_id = self.request.headers.get('X-Appengine-Inbound-Appid', None)
if app_id in AUTHORIZED_APPS:
super(MyHandler, self).dispatch()
else:
self.abort(403)
对于 header 中没有 X-Appengine-Inbound-Appid 的任何请求,这将引发 403。
此外,当使用 urlfetch 从一个应用程序向另一个应用程序发出请求时,请确保您设置了 follow_redirects=False,否则不会添加 header 。
关于security - 谷歌应用引擎 : secure inter-app communication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13245540/