GAE 为预定作业提供 cron 作业。如何设置一些安全措施以防止有人直接执行 http GET?在下面的示例中,我可以随时在浏览器的 url 字段中键入/updateData 以按照以下设置执行作业:
cron:
- description: daily update of the data in the datastore
url: /updateData
schedule: every day 00:00
timezone: ...
最佳答案
除了 Paul C 所说的之外,您还可以创建一个装饰器来检查 X-Appengine-Cron header ,如下所示。顺便说一句, header 不能被欺骗,这意味着如果不是来自 cron 作业的请求具有此 header ,App Engine 将更改 header 的名称。您还可以为任务编写类似的方法,在本例中检查 X-AppEngine-TaskName。
"""
Decorator to indicate that this is a cron method and applies request.headers check
"""
def cron_method(handler):
def check_if_cron(self, *args, **kwargs):
if self.request.headers.get('X-AppEngine-Cron') is None:
self.error(403)
else:
return handler(self, *args, **kwargs)
return check_if_cron
并将其用作:
class ClassName(webapp2.RequestHandler):
@cron_method
def get(self):
....
关于security - 谷歌应用引擎 : security of cron jobs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14193816/