【问题标题】:Google app engine task queue and users service谷歌应用引擎任务队列和用户服务
【发布时间】:2011-08-11 21:44:28
【问题描述】:

我正在使用 python 和 Google 应用引擎。我想使用任务队列。作为任务队列处理程序的一部分,我检查当前用户是否是管理员(使用用户服务)。这个测试总是失败。有没有办法让这个测试通过?

更新:为避免进一步混淆,我试图找出触发任务的用户是否是管理员(这只是一个简单的示例)。我了解该任务正在从服务器运行,并且所有用户 cookie 早已不复存在。所以我一直在寻找的答案是一种将会话转移到任务的方法

import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import taskqueue

class MyRequesHandler(webapp.RequestHandler):
    def get(self):
        taskqueue.add(url="/task/")

class MyTaskHandler(webapp.RequestHandler):
    def post(self):
        if users.is_current_user_admin():
            logging.debug("admin")
        else:
            logging.debug("not admin")


def main():
    logging.getLogger().setLevel(logging.DEBUG)
    application = webapp.WSGIApplication([
            ('/', MyRequesHandler),
            ('/task/', MyTaskHandler)
        ],
        debug=True)
    run_wsgi_app(application)

【问题讨论】:

    标签: google-app-engine task-queue


    【解决方案1】:

    Users API 反映了当前请求的登录用户的详细信息,显然在任务队列任务的情况下,没有用户,因为它是由任务队列系统发起的。您需要在将任务排入队列之前执行此检查,并将结果作为标志传递给任务。

    【讨论】:

      【解决方案2】:

      要代表某个特定任务执行任务,只需将数据存储 ID 或密钥(或用户的任何其他标识符)作为其负载传输给任务。

      class MyRequesHandler(webapp.RequestHandler):
          def get(self):
              taskqueue.add(url="/task/do_something", params={'user_email': users.get_current_user().email()})
      
      class MyTaskHandler(webapp.RequestHandler):
          def post(self):
              user_email = self.request.POST.get('user_email')
              user = User.all().filter('email', user_email).get()
              # ... do something on behalf of user
      

      这显然需要保护任务的 URL 免受外部未经授权的访问——这可以在 app.yaml 中完成:

      handlers:
          - url: /task/(.*)
            script: tasks.py
            login: admin
      

      Taskqueue 访问需要管理员登录的 URL 没有问题。

      【讨论】:

        【解决方案3】:

        执行任务队列项时,无法通过用户服务访问原始用户。但是,在创建任务时,您可以将原始用户的 ID 作为请求的一部分。

        为防止外部用户在任务执行期间伪造此用户 ID,请查找 X-AppEngine-QueueName 请求标头。此标头为only present on task queue items,外部用户无法伪造。

        【讨论】:

          猜你喜欢
          • 2012-11-13
          • 2015-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多